The first version of the integration testing framework was released in July of this year. It allowed you to deploy your application using the Jakarta Runtime of your choice to a Docker environment using the TestContainer framework.
Selecting the runtime is as easy as setting a member value of the test annotation, the runtime version can be specified, endpoints can be easily called based on a MicroProfile Rest Client interface definition and it allows to have remote debugging by just setting a flag.
In this version1.1, you have the option to define the Docker build file that will be used for the test, and features for testing microservices are added.
Custom builds
In the initial version, the official docker images of the Jakarta runtimes are used to run your application within the docker container. But in many cases, your application needs a specific image as it requires some additional content.
This can now be achieved by specifying a custom build file for Docker.
@ContainerIntegrationTest(runtime = SupportedRuntime.PAYARA_MICRO) @CustomBuildFile(location = "custom/payara") public class CustomPayaraIT extends AbstractContainerIntegrationTest { }
The annotation defines that the content of the directory src/docker/custom/payara
will be used to generate the Docker image that will be used to test your application. The build file is expected as a file called Dockerfile (if this is not present, the runtime default will be used) and all other files in that directory and subdirectories will be part of the build step. This allows you to have a fully customized Docker image for testing.
Of course, by defining the runtime like Payara Micro in the above example, the port assignment checks to see if the application is ready and debug setup is still taken from the indicated runtime. So make sure you specify the correct runtime in the @ContainerIntegrationTest
annotation which corresponds to the system used within the custom Docker build file.
This functionality is especially useful when you are working with Glassfish since they don’t have any official Docker image and support was limited in the first release for this runtime.
Microservices Testing
In most cases, your application depends on other services. They are called to retrieve data for or process parts of the functionality required for the user request. There are 2 additions available in this version that makes it easier to test your application in this situation.
Each public static variable that is a startle test container and added with @Container
is also started just before the execution of your test code. It is now easier in this version 1.1 when this additional container is running an application using one of the supported Jakarta runtimes. You only need to specify the docker image name and the setup like health checks and port mappings is taken care of for you automatically.
@Container public static GenericContainer<?> remote = new PayaraMicroContainer(DockerImageName.parse("test-remote:1.0"));
Also, networking configuration is taken care of, as it will share the same network as your application and this container will also have the name of the variable ‘remote’ in this case, as a hostname alias.
This makes it easy to add an external service needed for your application.
But instead of having an entire application providing data for your application, you can also configure the responses for URLs. In other words, you can mock the responses for certain requests for external services. For this purpose, Wiremock functionality is integrated into the framework.
First of all, you define a container that will be running the WireMock application where you also define the hostname which will be used.
@Container public static final WireMockContainer wireMockContainer = WireMockContainer.forHost("wire");
And within each test method, you can define the responses using a builder pattern
MappingBuilder mappingBuilder = new MappingBuilder() .forURL("/path") .withBody(foo); String mappingId = wireMockContainer.configureResponse(mappingBuilder);
The variable foo
can be a String and in that case, the response is a plain text response with the content of the variable as the body. Otherwise, the variable is serialised to JSON and a JSON-type response is prepared.
The builder also has some other methods to control the response like the HTTP status, media type, etc …
Integration testing
With the initial release, you had all the features required to test your application with the Jakarta runtime of your choice using Testcontainers and a docker environment.
In this update, the container running your application under test can now be fully customised using a build file and resources that you specify by indicating the directory where they can be found.
Also, testing applications that are part of a microservices environment got easier. When the eternal service is also a Jakarta application, you only need to indicate its docker image name, and all other configuration is taken care of for you by default.
You can also use Wiremock to define predefined responses for certain requests. Again, the network setup is greatly simplified, and defining responses is easy. JSON responses can be specified by providing the data as a Java Object that will be serialised so that you don’t need to write complex JSON strings.
Atbash Training and Support
Do you need a specific training session on Jakarta or MicroProfile? Or do you need some help in getting up and running with your next project? Have a look at the training support that I provide on the page https://www.atbash.be/training/ and contact me for more information.
Enjoy