Code testing automation of a Spring boot application using Gitlab CI and Docker

Code testing automation of a Spring boot application using Gitlab CI and Docker

Throughout the stages of an application development, the testing phase is a very important step that can be used to improve the logical structure of the code – for example by delegating specific tasks to different functions, thus making the code more clear and modular. Additionally, the testing phase addresses the analysis of the different inputs, in order for us to be able to adjust the code in a more secure manner. In this article we present how we structured the test automation processes for a Spring Boot application, by using Gitlab CI and Docker containers.

The application, which is developed in Java and managed with Maven package manager, provides a classic structure of the code organization, with the source codes located in src/main and tests in src/tests. We used the maven Surefire and JaCoCo plugins for the generation of the test results, and for reporting. The two plugins configuration in Maven’s pom.xml file is as follows:

configurazione dei due plugin nel file pom.xml di Maven

mvn clean verify

After running the tests through Maven command allows to verify that the files generated by Surefire and JaCoCo plugins are respectively located in target/surefire-reports and target/site/jacoco.

Additionally, in the jacoco folder we can find html files, which can be displayed through a browser, that indicate the status of the tests. At this point we structured GitLab in order to automate the testing phase. At the same level as Maven’s pom.xml file, we created a gitlab-ci.xml file that develops in three stages: testing, build and deployment.

Our application is based on Spring, and therefore on the dependency Injection. Thus, it is necessary to test it in two different ways:

  • By injecting a MySQL implementation of the defined repositories
  • By injecting a “Mock” implementation, where data is stored in the application memory.

Because of the “duality” of the code, we created two configuration.yml files for Spring (application-test-mock.yml and application-test-mysql.yml) that indicate the necessary configuration for loading respectively MySQL classes and Mock classes.

By using this command  is possible to run the class test command in the mock configuration. 

mvn verify

By using this command it is possible to run the tests in MySQL mode.

mvn verify

Therefore, the CI part in GitLab is structured as follows:

The test phase, here referred to as test-all, involves the configuration of a container with MySQL service, which is linked to the Maven container image where the application tests run. The Maven image includes the repository update and mysql client installation instructions, to connect to the MySQL container service and to then define the database arrangement through the script located in tools/sql/create_db.sql. Consequently, tests are launched with the maven commands described above.

Since that GitLab version 12.3.0 doesn’t support the coverage management through the JaCoCo plugin, it is then recommended to use the regex that GitLab offers in the project settings.

cat tagert/sites/jacoco/index.html

After launching the tests, it is possible to use this command  to print the results detected by JaCoCo, to allow GitLab’s coverage regex to capture them.

The coverage status will then be displayed in the pipeline, and will be available at the address: