Chapter 10. Testing your application

10.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.xml file.

Procedure

  1. Include the Thorntail BOM as described in Chapter 6, 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>
  2. Reference the io.thorntail:arquillian artifact in your pom.xml file with the <scope> set to test:

    <dependencies>
      <dependency>
        <groupId>io.thorntail</groupId>
        <artifactId>arquillian</artifactId>
        <scope>test</scope>
      </dependency>
    </dependencies>
  3. Create your Application.

    Write your application as you normally would; use any default project-defaults.yml files you need to configure it.

    thorntail:
      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-module-name: com.h2database.h2
            driver-xa-datasource-class-name: org.h2.jdbcx.JdbcDataSource
  4. Create a test class.

    Note

    Creating an Arquillian test before Thorntail existed usually involved programatically creating Archive due 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 {
    }
  5. Create a deployment.

    In the context of microservices, the entire application represents one small microservice component.

    Use the @DefaultDeployment annotation to automatically create the deployment of the entire application. The @DefaultDeployment annotation defaults to creating a .war file, which is not applicable in this case because Undertow is not involved in this process.

    Apply the @DefaultDeployment annotation 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 @DefaultDeployment annotation provided by Arquillian integration with Thorntail means you should not use the Arquillian @Deployment annotation on static methods that return an Archive.

    The @DefaultDeployment annotation 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 @DefaultDeployment annotation 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;
  6. 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 InitialContext of the running application into an instance member of the test case:

    @ArquillianResource
    InitialContext context;

    That means the test method itself can use that InitialContext to ensure the Datasource you configured using project-defaults.yml is live and available:

    @Test
    public void testDataSourceIsBound() throws Exception {
        DataSource ds = (DataSource) context.lookup("java:jboss/datasources/MyDS");
        assertNotNull( ds );
    }
  7. Run the tests.

    Because Arquillian provides an integration with JUnit, you can execute your test classes using Maven or your IDE:

    $ mvn install
    Note

    In many IDEs, execute a test class by right-clicking it and selecting Run.