jambit ToiletPaper 158 Icon

Embedded Database with Zonky

Problem

For integration testing, it has become common practice to use an H2 or a Docker container of the connected database. Another option is an embedded database. This has the advantage of being just as close to the productive database as a corresponding Docker container. At the same time, however, a container is no longer required and test execution is a bit more performant.

Solution

For Java projects, there is the project Zonky. This originally emerged as a fork from OpenTable Embedded PostgreSQL Component. Zonky supports embedded PostgreSQL DBs in integration testing. This version also spawned a version for the Spring project. See an example of how to embed Zonky in your integration tests in the following code snippets.

Zonky

Zonky supports these data providers:

  • Docker
  • Zonky
  • OpenTable
  • Yandex

All data providers support PostgreSQL. Docker additionally supports MSSQL, MySQL and MariaDB.

Liquibase as well as Flyway can be used normally.

Maven integration:

<dependency>
  <groupId>io.zonky.test</groupId>
  <artifactId>
    embedded-database-spring-test
  </artifactId>
  <version>2.1.1</version>
  <scope>test</scope>
</dependency>

Test using an embedded DB provided by Zonky:

@AutoConfigureEmbeddedDatabase          //Annotation of Zonky which leads the test to use an embedded DB
public class RepoIT {

    @Autowired
    private Repository repo;

    @Test
    public void ZonkyTest() {
        User user = new User(1, "Vorname", "Nachname");
        repo.save(user);
        Optional<User> found = repo.findById(user.getId());
        assertTrue(found.isPresent());
    }
}

Config in application.xml:

zonky.test.database.type=auto                # Possible values: AUTO, POSTGRES, MSSQL, MYSQL, MARIADB, H2
zonky.test.database.provider=default         # Possible values: DEFAULT, DOCKER, ZONKY, OPENTABLE, YANDEX, EMBEDDED
jambit ToiletPaper#158 Embedded database with Zonky

Further Aspects

---

Author: Patrick Hiesinger / Software Architect / Office Leipzig

Cookie Settings

This website uses cookies to personalize content and ads, provide social media features, and analyze website traffic. In addition, information about your use of the website is shared with social media, advertising, and analytics partners. These partners may merge the information with other data that you have provided to them or that they have collected from you using the services.

For more information, please refer to our privacy policy. There you can also change your cookie settings later on.

contact icon

Contact us now