Chapter 10. True-to-production integration testing

MicroShed Testing is a Java library that helps you write true-to-production integration tests for your application in an environment similar to production. To minimize the parity issues between development and production, you can test your application in the same Docker container that you use in production.

MicroShed Testing uses the Testcontainers framework to analyze your application from outside the Docker container without accessing the application internals. You can use MicroShed Testing to develop integration tests for your Open Liberty application.

You need to make sure that your application works accurately in production, similar to the development environment. Integration tests assess multiple test classes and components, but integration tests take longer to set up and configure than unit tests. Alternatively, unit tests are shorter and involve testing individual modules of an application. With shorter development cycles and limited time available, developers often run unit tests. MicroShed Testing helps you write and run true-to-production integration tests for your applications, and streamlines your integration tests with the Testcontainers framework for an efficient workflow.

10.1. Development and production parity

Development and production parity is one of the factors in the twelve-factor app, which is a methodology to build modern applications. The idea behind development and production parity is to keep the development, staging, and production environments similar, regarding time, personnel, and tools. To simplify the development process, developers often use tools in development that are different from production. For example, you might use a local Maven build to build a project for your application in development, but the application might be deployed to a Docker container in production. Differences between the environments can cause a test to fail in production, though the test passes in the development environment. MicroShed Testing helps achieve development and production parity by testing the application in an environment similar to production.

10.2. Writing integration tests with the Microshed Testing library

You can deploy your applications in different environments with containers. With the Testcontainers framework, you can use containers in a test environment. Microshed Testing is a Java library for MicroProfile and Jakarta EE developers to test their applications in an environment similar to production. Microshed Testing implements the Testcontainers framework to support development and production parity in testing.

With MicroShed Testing, you can write an integration test that looks like the following example:

@MicroShedTest
public class BasicJAXRSServiceTest {

    @Container
    public static ApplicationContainer app = new ApplicationContainer()
                    .withAppContextRoot("/myservice");

    @RESTClient
    public static PersonService personSvc;

    @Test
    public void testGetPerson() {
        Long bobId = personSvc.createPerson("Bob", 24);
        Person bob = personSvc.getPerson(bobId);

        assertEquals("Bob", bob.name);
        assertEquals(24, bob.age);
        assertNotNull(bob.id);
    }

    @Test
    public void testGetUnknownPerson() {
        assertThrows(NotFoundException.class, () -> personSvc.getPerson(-1L));
    }
}

The @MicroShedTest annotation searches for a Dockerfile document in the repository, starts up the application in a Docker container, and waits for the application to be ready before the test starts. The @Container annotation injects a REST Client proxy of the PersonService class, which helps you send HTTP requests on the running application container. The @RESTClient annotation sends an HTTP POST request to the running container, which triggers the PersonService#createPerson endpoint and returns the generated ID. By using the generated ID, you can send an HTTP GET request to read the record that was created. The JSON response automatically converts to a Person object by using a JSON-B token. The @Test annotation sends an HTTP GET request to find a Person object with the -1 ID, which does not exist. The annotation asserts that the application container returns an HTTP 404 exception.

10.3. See also

Guide: Testing a MicroProfile or Jakarta EE application