Contents
Overview
Helidon provides built-in test support for CDI testing in TestNG.
Maven Coordinates
To enable Testing with TestNG add the following dependency to your project’s pom.xml (see Managing Dependencies).
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-testng</artifactId>
<scope>test</scope>
</dependency>Usage
By default, a test can be annotated with io.helidon.microprofile.tests.testng.HelidonTest annotation 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.
A test can also be annotated with io.helidon.microprofile.tests.testng.HelidonTest annotation 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
A test can be annotated as follows:
@HelidonTest(resetPerTest = true)
This will change the behavior 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)
Usage - configuration
In addition to the @AddConfig annotation, you can also use @Configuration to configure additional classpath properties config sources using configSources, and to mark that a custom configuration is desired. If @Configuration(useExisting=true), the existing (or default) MicroProfile configuration would be used. In this case it is important to set property mp.initializer.allow=true in order 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)
Test method parameters are currently not supported.
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 .
app.greeting=Hello from resource
config_ordinal=1001 # value is bigger than fixed ordinal value `@AddConfig`@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"));
}
}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:
| Annotation | Usage |
|---|---|
@io.helidon.microprofile.tests.testng.AddBean | Used 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.testng.AddExtension | Used 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.testng.AddConfig | Used to add one or more configuration properties to MicroProfile config without the need of creating a microprofile-config.properties file |
@io.helidon.microprofile.tests.testng.DisableDiscovery | Used to disable automated discovery of beans and extensions |
@io.helidon.microprofile.tests.testng.Configuration | Switch between "synthetic" and "existing" configuration. Add classpath resources to the "synthetic" configuration |
Examples
In current example Helidon container will be launched prior 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.
@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"));
}
}