Integration Testing with Docker Maven Plugin, PostgreSQL, Flyway (Update)

Recently I have written about how one can create a PostgreSQL Docker image with the Docker Maven Plugin to run integration tests that require a database. While this worked all nice and well during development, the concept has one flaw: the plugin will push the database Docker image to a Docker registry during the deploy phase. I do not want this temporary image to end up there. This is the behavior of the Docker Maven Plugin and I have not found a way to work around this. By “work around this” I mean somehow configure the plugin to ignore this custom PSQL image during the “deploy” phase. Unfortunately, there is only a global <filter> that applies to all the phases of the plugin.

There is a proper solution however, at least for what I was using the database.

Read More »

Integration Testing With Docker Maven Plugin, PostgreSQL, Flyway

Some things in software development require more than mocks and unit testing. If your application uses a database it makes sense to also hit that database in automated testing to ensure custom SQL queries work correctly, Hibernate relations are set up properly and also that database migrations are successful.

This blog post was written with a focus on the latter. I will be using Spring Boot talking to a PostgreSQL database. The database structure is managed via Flyway and, basically customary for Java applications, Maven serves as the build and dependency management tool. Docker will also play a role because we’ll be creating and running a PostgreSQL docker image for testing. From Maven. Every time the test is executed. And to spice things up, we’ll also create a custom database and user in that dockerized PSQL image.

I have created a working sample on Github and you can follow every single step by taking a look at the commit history. There you can see individual changes, starting from an empty Spring Boot application with no database to the final solution with Spring Data JPA and Flyway.

In the following sections and snippets, I will highlight the important parts of each step.

Read More »

Using Groovy Spock in a Maven Java Project

Groovy Spock is a testing framework that can be used as an alternative to the venerable JUnit. In Java projects it’s probably very common (I don’t have any data, just an assumption based on how I think) to also use a Java based testing framework. The most widely known is JUnit, although not the only one of its kind (e.g. see this article on DZone). However, Java’s syntax can sometimes be rather cumbersome and verbose, and this is where a dynamic language like Groovy can help. It is often used to create nice and interesting DSLs, e.g. as the basis of the Gradle project or, as in the case of Spock, for testing.

Here’s how to integrate the Groovy Spock testing framework in a Maven based Java project.

One thing up front: I’m no fan of Groovy. I’ve worked with Grails projects for several years and using Groovy has more than once proven to be a problem. Especially in very large applications. However, I do see the benefits it can provide in certain situations and I have come to like the more expressive, although sometimes odd to read, Spock DSL in tests.

Read More »