Testing Jakarta EE 9 Components With Arquillian and JBoss Weld
Tags: Jakarta Ee, Java, Java Ee, Cdi, Weld

Configuring Arquillian Weld Container

Add the newest arquillian-weld-embedded dependency into the project pom.xml file.

<dependency>

<groupId>org.jboss.arquillian.container</groupId>

<artifactId>arquillian-weld-embedded</artifactId>

<version>${arquillian-weld-embedded.version}</version>

<scope>test</scope>

</dependency>

And also add the weld runtime into dependencies.

<dependency>

<groupId>org.jboss.weld</groupId>

<artifactId>weld-core-impl</artifactId>

<version>${weld.version}</version>

<scope>test</scope>

</dependency>

In the Arquillian configuration file arquillian.xml , add a container specific configuration for Weld.

<container qualifier="arq-weld">

<configuration>

<property name="enableConversationScope">true</property>

<property name="environment">SE</property>

</configuration>

</container>

The enableConversationScope property allows you decide if use @ConversationScope in the tests. And there are several environment available, check the source code of Environments.

As a CDI container, we can not run all Jakarta EE compoennts with Weld, for example, the Jakarta Restul WebService resources.

So let’s exclude the components that can not be run in the Weld containers, and focus on testing CDI components.

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-failsafe-plugin</artifactId>

<version>${maven-failsafe-plugin.version}</version>

<configuration>

<systemPropertyVariables>

<arquillian.launch>arq-weld</arquillian.launch>

</systemPropertyVariables>

<excludes>

<exclude>**/it/GreetingResourceTest*</exclude>

</excludes>

</configuration>

</plugin>

In our sample project, there are two integration tests will be run on Arquillian containers, let’s exclude the GreetingResourceTest which is use to expose Restful APIs.

The left is GreetingServiceTest , which is to test the functionality of a simple CDI bean GreetingService .

@ExtendWith(ArquillianExtension.class)

public class GreetingServiceTest {

private final static Logger LOGGER = Logger.getLogger(GreetingServiceTest.class.getName()); @Deployment

public static JavaArchive createDeployment() {

return ShrinkWrap.create(JavaArchive.class)

.addClass(GreetingMessage.class)

.addClass(GreetingService.class)

.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

} @Inject

GreetingService service; @Test

@DisplayName("testing buildGreetingMessage")

public void should_create_greeting() {

LOGGER.log(Level.INFO, " Running test:: GreetingServiceTest#should_create_greeting ... ");

GreetingMessage message = service.buildGreetingMessage("Jakarta EE");

assertTrue(message.getMessage().startsWith("Say Hello to Jakarta EE at "),

"message should start with \"Say Hello to Jakarta EE at \"");

}

}

In the above codes,

The new @ExtendWith(ArquillianExtension.class) replaces the old @RunWith(Arquilian.class) in the Jakarta EE 8 version.

replaces the old in the Jakarta EE 8 version. The @Test annotation is from the org.junit.jupiter.api package which belongs to JUnit 5.

annotation is from the package which belongs to JUnit 5. The @DisplayName annotation allows IDEs or other tools to use a friendly describable text instead of the method name in the test reporting results.

annotation allows IDEs or other tools to use a friendly describable text instead of the method name in the test reporting results. The @Deployment describes the assembly resources of the deployment archive for this test. Similar with general Jakarta EE components, you can use @Inject beans in Arquillian test classes. These two items are no difference from the Jakarta EE 8 starter.

Running tests

Open a terminal window, enter the project root folder, execute the following command.

mvn clean verify -Parq-weld

In the Jakarta EE 9 sample project, I usually use a Maven profile to organize the resources of testing against a Arquillian container. Here arq-weld profile is configured for Weld container.

NOTE: In the real world application development, it is an excellent practice to use Maven profiles to categorize the configurations for different environments.

You will see the following info in the console.