I'm building a Spring Boot app, backed by Postgres, using Flyway for database migrations. I've been bumping up against issues where I cannot produce a migration that generates the desired outcome in both Postgres, and the embedded unit test database (even with Postgres compatibility mode enabled). So I am looking at using embedded Postgres for unit tests.
I came across an embedded postgres implementation that looks promising, but don't really see how to set it up to run within Spring Boot's unit test framework only (for testing Spring Data repositories). How would one set this up using the mentioned tool or an alternative embedded version of Postgres?
The configuration below works well with Spring Boot 2.0.
The advantage over embedded-database-spring-test is that this solution doesn't push Flyway into the classpath, possibly messing up Spring Boot's autoconfiguration.
Maven:
The class is based on the code I found here: https://github.com/nkoder/postgresql-embedded-example
I modified it to use
HikariDatasource
(Spring Boot's default) for proper connection pooling. ThebinariesDir
anddataDir
are used to avoid costly extraction+initdb in repeated tests.Take a look at this: https://github.com/zonkyio/embedded-database-spring-test. Just to be clear, it's meant for integration testing. Meaning the Spring context is initialised during the individual test.
As per the tools documentation, all you need to do is to place
@AutoConfigureEmbeddedDatabase
annotation above class:and add Maven dependency:
To use it together with
@DataJpaTest
you need to disable the default test database by using the annotation@AutoConfigureTestDatabase(replace = NONE)
:To make the use more comfortable you could also create a composite annotation, something like:
..and then use it above your test class:
You can try https://github.com/TouK/dockds. This auto-configures a docker contained database.
I'm the author of the embedded-database-spring-test library that was mentioned by @MartinVolejnik. I think the library should meet all your needs (PostgreSQL + Spring Boot + Flyway + integration testing). I'm really sorry that you're having some trouble, so I've created a simple demo app that demonstrates the use of the library together with Spring Boot framework. Below I have summarized the basic steps that you need to do.
Maven configuration
Add the following maven dependency:
Flyway configuration
Add the following property to your application configuration:
Further, make sure that you do not use
org.flywaydb.test.junit.FlywayTestExecutionListener
. Because the library has its own test execution listener that can optimize database initialization and this optimization has no effect if theFlywayTestExecutionListener
is applied.Spring Boot 2 Configuration
Since Spring Boot 2, there is a compatibility issue with Hibernate and Postgres Driver. So you may need to add the following property to your application configuration to fix that:
Example
An example of test class demonstrating the use of the embedded database: