Unit Tests

You can easily write Unit Tests in the Axon Ivy Designer to test your Java source code using JUnit 5. If your code uses the Ivy environment you can either mock it or use the @IvyTest annotation that correctly sets up the environment.

How to Write Unit Tests

To get started you have to create a test project. The ‘Axon Ivy Test Project’ wizard will help you to create a test project with all required configurations plus a simple sample test.

  1. Add a new Axon Ivy Test Project

    • Right click on a project you would like to write tests for.

      Select: New -> Axon Ivy Test Project in the context menu.

    • Pick the test flavor IvyTest to include in the ‘New Axon Ivy Test Project’ wizard.

    • Finish the wizard.

      A sample test will be generated into the src_test directory of the newly created project.

  1. Run test

    • Make sure the Axon Ivy Engine is started with your projects

    • To run this test right-click inside this class -> Run As -> JUnit Test

    • A new JUnit View should be opened and the test should be green.

This is all you need to start writing conventional Unit Tests.

Set Up the Axon Ivy Environment

To gain access to the Ivy context the SampleIvyTest class is annotated with the @IvyTest annotation.

Note

If you run your test with @IvyTest (without process) the bpm engine does not get started. This speeds up test execution.

Write a Unit Test

Here we test a class called OrderUtil. This class needs access to different Ivy resources like Variables. Without properly setting up the Ivy environment we would have to mock all those resources.

@Test
void products() {
  /* OrderUtil should return two products */
  assertThat(OrderUtil.getProducts()).hasSize(2);
  Product table = OrderUtil.getProducts().get(0);
  Product chair = OrderUtil.getProducts().get(1);

  /* The first product is a table and costs 375.50 */
  assertThat(table.getName()).isEqualTo("Table");
  assertThat(table.getSinglePrice()).isEqualTo(375.5);

  /* The second product is a chair and costs 89.60 */
  assertThat(chair.getName()).isEqualTo("Chair");
  assertThat(chair.getSinglePrice()).isEqualTo(89.60);
}

Tip

Have a look at the demo project to see what else is possible.

Change Application Runtime

For some test cases, you may need to use a different value for a Variable or change an app configuration. In this case use AppFixture to change the value. The value gets automatically reset to the original value after the test execution. All you have to do is to tell the test that you want an instance of the AppFixure class. Let’s have a look at it:

@Test
void products_variable(AppFixture fixture) {
  /* The AppFixture can manipulate variables. */
  fixture.var("table", "799.95");

  assertThat(OrderUtil.getProducts()).hasSize(2);
  Product table = OrderUtil.getProducts().get(0);

  /* Normally the price of a table would be 375.50 */
  assertThat(table.getSinglePrice()).isEqualTo(799.95);
}

Or you can change an app configuration, e.g change the URL of a RestClient:

@Test
void products_config(AppFixture fixture) {
  assertThat(Ivy.rest().client("jsonPlaceholder").getUri().toString()).isEqualTo("https://jsonplaceholder.typicode.com/");
  /* The AppFixture can manipulate app configurations. */
  fixture.config("RestClients.jsonPlaceholder.Url", "${ivy.app.baseurl}/api/testMock");
  assertThat(Ivy.rest().client("jsonPlaceholder").getUri().toString()).isEqualTo("http://localhost:8080/test/api/testMock");
}

Furthermore it is possible to use the AppFixture to login with an existing user. You can either login with a user name or with an IUser object. In the example below we injected the IUser object as a method parameter.

@Test
void products_userLogin(AppFixture fixture, @Named("M") IUser user) {
  /* Login with a username */
  fixture.loginUser("James Bond");
  var products = OrderUtil.getProductsWithClearance();

  /* James Bond can only order chairs */
  assertThat(products).hasSize(1);
  assertThat(products.get(0).getName()).isEqualTo("Chair");

  /* Login with an IUser object that we injected as a parameter */
  fixture.loginUser(user);
  products = OrderUtil.getProductsWithClearance();

  /* M can order tables and chairs */
  assertThat(products).hasSize(2);
  assertThat(products.get(0).getName()).isEqualTo("Table");
  assertThat(products.get(1).getName()).isEqualTo("Chair");
}