Skip to content

Instantly share code, notes, and snippets.

@jakzal
Last active October 28, 2020 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakzal/db2e6865cb11a3a6db6c47fab5638474 to your computer and use it in GitHub Desktop.
Save jakzal/db2e6865cb11a3a6db6c47fab5638474 to your computer and use it in GitHub Desktop.
Test containers with Spring Boot
spring:
jpa:
hibernate:
dialect: org.hibernate.dialect.Postgres95Dialect
ddl-auto: none
show-sql: true
datasource:
url: ${DB_URL}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
schema: classpath:db/schema/schema-postgresql.sql
initialization-mode: always
liquibase:
drop-first: true
package acme.testcontainers;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
public class DatabaseContainerExtension implements BeforeAllCallback {
private final PostgreSQLContainer postgreSQLContainer = SharedPostgreSQLContainer.getInstance();
@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
postgreSQLContainer.start();
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<!-- ... -->
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.11.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- ... -->
</project>
package acme.repository;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import acme.testcontainers.DatabaseContainerExtension;
import javax.validation.OverridesAttribute;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(DatabaseContainerExtension.class)
@Documented
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Tag("slow")
public @interface RepositoryTest {
@OverridesAttribute(constraint = SpringBootTest.class, name = "properties")
String[] properties() default {"spring.main.banner-mode=off"};
}
package acme.testcontainers;
import org.testcontainers.containers.PostgreSQLContainer;
public class SharedPostgreSQLContainer extends PostgreSQLContainer<SharedPostgreSQLContainer> {
private static final String IMAGE_VERSION = "postgres:9.6";
private static SharedPostgreSQLContainer container;
private SharedPostgreSQLContainer() {
super(IMAGE_VERSION);
}
public static SharedPostgreSQLContainer getInstance() {
if (container == null) {
container = new SharedPostgreSQLContainer();
}
return container;
}
@Override
public void start() {
super.start();
System.setProperty("DB_URL", container.getJdbcUrl());
System.setProperty("DB_USERNAME", container.getUsername());
System.setProperty("DB_PASSWORD", container.getPassword());
}
@Override
public void stop() {
// do nothing, the JVM handles shut down
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment