Integration Tests should be part of a project from early beginning till the very end. There are many undeniable advantages of using continuous testing process. Ceaseless development of tests allow to avoid regression.
To avoid mixing business purposes of Integration Testing, let’s concentrate only at technical details of proposed solution.
What will be used during this example:
- Docker for Windows
- Docker Compose
- Docker Gradle Plugin
- Docker Compose Gradle Plugin
To assure during gradle build that Docker Images will be created from your backend, just use Docker Gradle Plugin. If want to run Integration Tests based on created docker images, then use Docker Compose Gradle Plugin. Below is a small example of simple backend application and then detailed description of integrating docker to the tests layer. Note, that such a tests should be separate step during testing process and may not be a part of standard gradle build process.
We will build example application that is a standalone JAR file with running http rest server and one health check endpoint. This application will use listed above technologies for Integration Tests. To assume that backend application is simple, will be used also:
First step is to create a simple backend.
Let’s create a fresh Gradle Project. This project should have folder and gradle.build inside of it. We would need to have a submodule for a backend application. Backend will not be a root project itself, because later there will be need to add more submodules. At this stage we should have a folder for example project and a subfolder with backend application inside of it. This example should not concentrate on architecture of backend, so we will create only few classes backend application with one rest endpoint.
Example project should contain files:
You should after import to development environment see something like:
After starting the Main you should see logs:
After running GET in the browser you should see:
As described in example, server of this backend is using port 9999 and endpoint “/check” to provide response with “OK” text.
Details about used solutions for constructing backend application in similar schema can be found in related posts and articles.
Docker on a way to test
In different solution like Integration Tests with heavy application in JUnit or Nested Tests in JUnit probably you would start your application in the code and close it at the end of tests. Such a way for testing is also proper, however it is not vitalized in any way. In this example our goal is not only to provide virtualization of testing environment but also perform more tests like loadtesting. What are advantages of dockerized? There are many, they are in details described in other articles. Most important is: virtualization.
Compiling of docker images during building
In example there is already used flatJar plugin in gradle, that will provide backend as a standalone runnable JAR file with dependencies. Goal is to create Docker Image with such a JAR running on it and serving rest services on the selected port. To achieve this, there is need to use Gradle Docker Plugin that will compile docker image with selected artifact from backend module.
In main build.gradle let’s add definition of applyDocker method.
It will use /additions/gradle-files/docker.gradle and base docker image alpine-java.
For details about this solution see detailed description at plugin homepage:
Now our gradle and plugin is almost ready. Still there is need to add docker definitions to build images. As our backend will use some existing Docker Image and will just extend it, then we need to provide that definition.
There is also need to add new module in settings.gradle
Provide target Docker Images definitions:
In this definition is used file start.sh, so we need to provide it too.
Finally add Dockerfile as an definition to our backend project:
Our project is now Dockerized. Let’s build it and have Docker Images after build in local docker repository. Remember that in your system there has to be installed “Docker”.
Now able to build a project with gradle and use Docker Image from it.
Run the Integration Tests
To properly run integration tests, there is need to use Gradle Docker Compose Plugin.
Add a new module project called in example integration-tests. The build.gradle may have content based on:
After executing on your local or CI:
gradle clean build docker integrationTest
Gradle will take care about compiling JAR, Docker and run classes in the “integrationTests” packages in your project.
Load Tests during normal development flow
Properly configured above example allow to use Engulf and it’s API to check endpoints against high load.
Remember that you can always use Engulf in many instances, by providing proper docker-compose.xml. See documentation at gradle plugin webpage of how to use docker-compose files properly.
Additional descriptions will be available in separate posts. This technical description is not a complete “How To”, it’s purpose is only to show possibilities of using docker with integration testing and continuous testing. Probably you may also be interested in using such a solution to deploy and post check on environments. Same scenarios properly used may be ideal for post-deploy checking of environment integrity.