Chapter 6. Testing
6.1. Testing in a container
Using Arquillian, you have the capability of injecting unit tests into a running application. This allows you to verify your application is behaving correctly. There is an adapter for Thorntail that makes Arquillian-based testing work well with Thorntail–based applications.
Prerequisites
-
A Maven-based application with a
pom.xmlfile.
Procedure
Include the Thorntail BOM as described in Section 4.5, “Using a BOM”:
<dependencyManagement> <dependencies> <dependency> <groupId>io.thorntail</groupId> <artifactId>bom</artifactId> <version>${version.thorntail}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Reference the
io.thorntail:arquillianartifact in yourpom.xmlfile with the<scope>set totest:<dependencies> <dependency> <groupId>io.thorntail</groupId> <artifactId>arquillian</artifactId> <scope>test</scope> </dependency> </dependencies>Create your Application.
Write your application as you normally would; use any default
project-defaults.ymlfiles you need to configure it.swarm: datasources: data-sources: MyDS: driver-name: myh2 connection-url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE user-name: sa password: sa jdbc-drivers: myh2: driver-class-name: org.h2.Driver xa-datasource-name: org.h2.jdbcx.JdbcDataSource driver-module-name: com.h2database.h2Create a test class.
NoteCreating an Arquillian test before Thorntail existed usually involved programatically creating
Archivedue to the fact that applications were larger, and the aim was to test a single component in isolation.package org.wildfly.swarm.howto.incontainer; public class InContainerTest { }Create a deployment.
In the context of microservices, the entire application represents one small microservice component.
Use the
@DefaultDeploymentannotation to automatically create the deployment of the entire application. The@DefaultDeploymentannotation defaults to creating a.warfile, which is not applicable in this case because Undertow is not involved in this process.Apply the
@DefaultDeploymentannotation at the class level of a JUnit test, along with the@RunWith(Arquillian.class)annotation:@RunWith(Arquillian.class) @DefaultDeployment(type = DefaultDeployment.Type.JAR) public class InContainerTest {Using the
@DefaultDeploymentannotation provided by Arquillian integration with Thorntail means you should not use the Arquillian@Deploymentannotation on static methods that return anArchive.The
@DefaultDeploymentannotation inspects the package of the test:package org.wildfly.swarm.howto.incontainer;
From the package, it uses heuristics to include all of your other application classes in the same package or deeper in the Java packaging hierarchy.
Even though using the
@DefaultDeploymentannotation allows you to write tests that only create a default deployment for sub-packages of your application, it also prevents you from placing tests in an unrelated package, for example:package org.mycorp.myapp.test;
Write your test code.
Write an Arquillian-type of test as you normally would, including using Arquillian facilities to gain access to internal running components.
In the example below, Arquillian is used to inject the
InitialContextof the running application into an instance member of the test case:@ArquillianResource InitialContext context;
That means the test method itself can use that
InitialContextto ensure the Datasource you configured usingproject-defaults.ymlis live and available:@Test public void testDataSourceIsBound() throws Exception { DataSource ds = (DataSource) context.lookup("java:jboss/datasources/MyDS"); assertNotNull( ds ); }Run the tests.
Because Arquillian provides an integration with JUnit, you can execute your test classes using Maven or your IDE:
$ mvn install
NoteIn many IDEs, execute a test class by right-clicking it and selecting
Run.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.