Testcontainers offers several initialization strategies for our Docker containers when writing integration tests. Depending on the Docker image we use for our tests, we might have to perform additional setup steps. This includes adjusting the container configuration or populating data. With Testcontainers, we can tweak the container configuration either during runtime (executing commands inside the container) or before starting it. With this blog post, we’ll look at several of these strategies to configure the Docker container for our integration tests. By Philip.
When writing integration tests that involve a database, we need a solution to initialize our database container. With Testcontainers, we can define an init script that is executed as part of the container initialization: .withInitScript(). Using Spring Boot and Spring Data JPA with Flyway or Liquibase, the database schema migration tool can create the database schema for us.
The article contents is split:
- Execute commands inside the container with Testcontainers
- Mount files into our container for the integration test
- Use an InitScript to initialize our container
- Use a prepopulated container with Testcontainers
You can find further Testcontainers-related tips & tricks in the linked articles. Nice one![Read More]