Last active
October 28, 2020 17:08
-
-
Save jakzal/db2e6865cb11a3a6db6c47fab5638474 to your computer and use it in GitHub Desktop.
Test containers with Spring Boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment