Contents

Overview

Helidon provides built-in test support for CDI testing in JUnit5.

Maven Coordinates

To enable Testing with JUnit add the following dependency to your project’s pom.xml (see Managing Dependencies).

<dependency>
    <groupId>io.helidon.microprofile.tests</groupId>
    <artifactId>helidon-microprofile-tests-junit5</artifactId>
    <scope>test</scope>
</dependency>
Copied

Usage

A test can be annotated with io.helidon.microprofile.tests.junit5.HelidonTest to mark it as a CDI test. This annotation will start the CDI container before any test method is invoked, and stop it after the last method is invoked. This annotation also enables injection into the test class itself.

Usage - per method CDI container

When a test is annotated with @HelidonTest(resetPerTest = true), the behavior will change as follows:

  • A new CDI container is created for each test method invocation

  • annotations to add config, beans and extension can be added for each method in addition to the class

  • you cannot inject fields or constructor parameters of the test class itself (as a single instance is shared by more containers)

  • you can add SeContainer as a method parameter of any test method and you will get the current container

Usage - configuration

In addition to the @AddConfig annotation, you can also use @Configuration to configure additional classpath properties and configSources to mark that a custom configuration is desired.

If @Configuration(useExisting=true), the existing (or default) MicroProfile configuration would be used. it is important to set property mp.initializer.allow=true in order for the CDI container to start, when used with @HelidonTest.

You can set up config in @BeforeAll method and register it with ConfigProviderResolver using MP Config APIs, and declare @Configuration(useExisting=true). Note that this is not compatible with repeatable tests that use method sources that access CDI, as we must delay the CDI startup to the test class instantiation (which is too late, as the method sources are already invoked by this time).

If you want to use method sources that use CDI with repeatable tests, please do not use @Configuration(useExisting=true)

Configuration Ordering

The ordering of the test configuration can be controlled using the mechanism defined by the MicroProfile Config specification.

The configuration expressed with @AddConfig has a fixed ordinal value of 1000

By default @Configuration.configSources has lower priority than @AddConfig. It can be changed via config_ordinal .

Test resource (additional-config.properties)
app.greeting=Hello from resource
config_ordinal=1001 # value is bigger than fixed ordinal value `@AddConfig`
Copied
Code sample
@HelidonTest
@AddConfig(key = "app.greeting", value = "Hello from annotation")
@Configuration(configSources = "additional-config.properties")
class TestExample {
    @Inject
    @ConfigProperty(name = "app.greeting")
    private String message; // property from `additional-config.properties` will be injected

    @Test
    void testGreeting() {
        assertThat(message, is("Hello from resource"));
    }
}
Copied

Usage - added parameters and injection types

The following types are available for injection (when a single CDI container is used per test class):

  • WebTarget - a JAX-RS client’s target configured for the current hostname and port when helidon-micorprofile-server is on the classpath

The following types are available as method parameters (in any type of Helidon tests):

  • WebTarget - a JAX-RS client’s target configured for the current hostname and port when helidon-micorprofile-server is on the classpath

  • SeContainer - the current container instance

API

The annotations described in this section are inherited (for the non-repeatable ones), and additive (for repeatable). So if you declare @DisableDiscovery on abstract class, all implementations will have discovery disabled, unless you annotate the implementation class with @DisableDiscovery(false). If you declare @AddBean on both abstract class and implementation class, both beans will be added.

In addition to this simplification, the following annotations are supported:

AnnotationUsage
@io.helidon.microprofile.tests.junit5.AddBeanUsed to add one or more beans to the container (if not part of a bean archive, or when discovery is disabled)
@io.helidon.microprofile.tests.junit5.AddExtensionUsed to add one or more CDI extensions to the container (if not added through service loader, or when discovery is disabled)
@io.helidon.microprofile.tests.junit5.AddConfigUsed to add one or more configuration properties to MicroProfile config without the need of creating a microprofile-config.properties file
@io.helidon.microprofile.tests.junit5.DisableDiscoveryUsed to disable automated discovery of beans and extensions
@io.helidon.microprofile.tests.junit5.ConfigurationSwitch between "synthetic" and "existing" configuration. Add classpath resources to the "synthetic" configuration
@io.helidon.microprofile.tests.junit5.SocketCDI qualifier to inject a JAX-RS client for a named socket

Examples

In this example, the Helidon container will be launched before the test. The Bean Discovery will be disabled. MyBean will be added to the test, so that it can be injected. ConfigCdiExtension will be enabled for this test. And finally, a configuration property will be added using @AddConfig annotation.

Code sample
@HelidonTest
@DisableDiscovery
@AddBean(MyBean.class)
@AddExtension(ConfigCdiExtension.class)
@AddConfig(key = "app.greeting", value = "TestHello")
class TestExample {
    @Inject
    private MyBean myBean;

    @Test
    void testGreeting() {
        assertThat(myBean, notNullValue());
        assertThat(myBean.greeting(), is("TestHello"));
    }
}
Copied

Additional Information

Reference