Unit and Integration Tests against a real database with Continuous Integration

Andrea Tarquini
Javarevisited
Published in
3 min readAug 28, 2019

A real project example using Github, Travis CI, Java/Spring, and PostgreSQL.

I’m going to explain a very simple free solution that I really use with my ambitious project called Gemini that automatically generate backend REST API from scratch starting from a simple DSL model.

In Gemini I made a lot of opinionated actions to translate the abstract type I defined to real SQL statements and queries, sometimes using some database-specific features (such as domains in PostgreSQL).

I developed a lot of integration tests that use a real PostgreSQL database and each time a macro feature should be tested I completely erase the integration test database starting from a fresh one. I know this process may take some times to complete, but It is not important. I rarely run all the codebase tests while I’m programming. I run only sections that may be affected and Travis and Github will notice me if something goes wrong at all.

So let’s go in deep but I don’t want to explain how I start Spring context or erase the database here you could contact me or navigate through the code of Gemini if you need help with Spring/Gradle/Java.

Instead, I want to explain how to set up and use a combination of Github and Travis to start the integration tests. In this way, if you don’t use Java or Spring you can set up the continuous integration process replacing the language/commands with yours.

First of all you need to enable the Travis Webhook inside your Github repository settings (so you need a Travis account).

Then simply provide a suitable .travis.yml in the root of your project. The following is mine for Gemini.

It is really easy:

  1. Define the language you are using. In my case java and openjdk 10
  2. Then install the required tools for testing and the database inside the before_install section. I need gradle and postgreslq11
  3. before_script is to initialize services. I create the database that I’m using in my integration tests to connect with
  4. script is the section that starts the test flow. For example i clean all the gradle context and I run a gradle task jacocoRootReport that compile all and start tests for each submodulo of my Spring project

That’s all. Github and Travis will do the rest. Take a look for example at the result of a pull request Merge…

--

--

Andrea Tarquini
Javarevisited

#software #developer and #engineer ➙ Proud #italian 🇮🇹 #geek ➙ #fullstack and #maker for fun