Testing

Testing an UShip application

Testing an UShip application just requires to launch a CDI container. By default we rely on org.apache.openwebbeans:openwebbeans-junit5 to do so. It enables to start the container marking the test class with @Cdi:

@Cdi (1)
class MyTest {
    @Inject (2)
    @JsonRpc
    private SomeJsonRpcController controller;

    @Test (3)
    void doTest() {
        // ...
    }
}
  1. Mark the test class to need a CDI SE container,

  2. @Cdi makes the test instance injections aware,

  3. Then simply write your tests as usual.

Tip
@Cdi has some configuration options. One of the most interesting is to set reusable = true. It will enable to use the same container for all tests. Since, with this mode, all tests of the suite (configurable in surefire) must use the same flag, you can define a JUnit 5 stereotype:
@Target(TYPE)
@Retention(RUNTIME)
@TestInstance(PER_CLASS)
@Cdi(reusable = true)
public @interface MyAppSupport {
}

Now, instead of using @Cdi, you can use @MyAppSupport on test classes avoiding to miss a reusable = true configuration. It is also very useful to add extension to all tests at once, for example if you have some JPA enhancer to execute before the container starts or some entity spying to auto delete test data between test, you can add the extensions there:

@Target(TYPE)
@Retention(RUNTIME)
@TestInstance(PER_CLASS)
@Cdi(reusable = true)
@ExtendWith(OpenJPATestEnhancer.class)
@ExtendWith(AutoEntityDeletion.class)
public @interface MyAppSupport {
}

The test will then not change:

@MyAppSupport
class MyTest {
    // same as before
}