Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

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>
    Copy to Clipboard Toggle word wrap
  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>
    Copy to Clipboard Toggle word wrap
  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
    Copy to Clipboard Toggle word wrap
  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 {
    }
    Copy to Clipboard Toggle word wrap
  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 {
    Copy to Clipboard Toggle word wrap

    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;
    Copy to Clipboard Toggle word wrap

    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;
    Copy to Clipboard Toggle word wrap
  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;
    Copy to Clipboard Toggle word wrap

    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 );
    }
    Copy to Clipboard Toggle word wrap
  7. Run the tests.

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

    $ mvn install
    Copy to Clipboard Toggle word wrap
    Note

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

Nach oben
Red Hat logoGithubredditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2025 Red Hat