Since I assume you already know Spring Boot and the benefits of this one,this example will be exclusively focused on showing how to write acceptance tests agains a virtual environment that runs on docker containers and they are orchestrated by compose (previously known as flig).
The example will consist on building a couple of micro services: One will persist data into the data base and the second one will take this information from the database. To sum up we will get 3 containers (1 per each micro service and the third for the database) which are represented on the below:
The purpose of this article is run our acceptance tests for a couple of micro services which are installed on docker containers, and being orchestrated by Compose (previously known as flig).
Let’s do it
First of all, we will create our Spring micro services. We can do it taking advantage of this site https://start.spring.io/ which I found recently and It is so handy since it provides us with a skeleton of a spring boot project.
You can find the code on the following link
If you download the code you will see a maven project with three submodules:
- ms.insertion: Micro service I : Insert items into the database.
- ms.extraction: Micro service II : Extract items from the database.
- ms.at: Acceptance Tests project.
Each micro service module contains a couple of integration tests which you can run by the following command: mvn integration-test -P integration
Now, that we already have our two micro services correctly implemented, we will create our acceptance tests making use of cucumber. Below we can see the feature
Until few time ago we would run the acceptance tests by following the below steps:
- Launching the web server
- Deploying the war onto the application server.
- Running our acceptance tests agains
- Stopping the application server.
The above way to run our acceptance tests is fine but not great since we would be running our acceptance tests in an environment completely different to production.
In the last year many people are betting for working with containers to simulate a production environment, so please do not forget about testing and take advantage of this to move another step forward.
Below some pros and cons of doing acceptance tests as is explained in this article:
- Testing in a real environment.
- Adapting our Tests to the new way to do things.
- Improving our continuous integration.
- Adding quality to our developments.
Introduce developers to a DevOps world.
- This could take bit longer than running our tests in our local environment.
- Developers need to have basic knowledge about docker.
You can find the code on my github repo link
The steps to run the example are explained on Git repository (README.md), Be aware of the first time you run the example this will take a little more as the virtual environment is created. In this example, I assume you have vegrant installed on your computer.
I am still working on this example but in the meantime I encourage you to fork my project and make some pull requests!