Skip to content

Latest commit

 

History

History
39 lines (31 loc) · 1.04 KB

File metadata and controls

39 lines (31 loc) · 1.04 KB

Spring JPA with PostgreSQL

This is all you need to set up PostgreSQL container inside Spring Boot Test with JUnit 5.

Test code

@SpringBootTest
@Testcontainers
internal class CatControllerTest {
    companion object {
    	@Container
    	@JvmStatic
    	val postgreSQLContainer = PostgreSQLContainer()
    
    	@DynamicPropertySource
    	@JvmStatic
    	fun postgreProperties(registry: DynamicPropertyRegistry) {
    		registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl)
    		registry.add("spring.datasource.username", postgreSQLContainer::getUsername)
    		registry.add("spring.datasource.password", postgreSQLContainer::getPassword)
    	}
    }
}

Full code

Gradle dependencies

dependencies {
    implementation(platform("org.testcontainers:testcontainers-bom:1.19.5"))
    testImplementation("org.testcontainers:junit-jupiter")
    testImplementation("org.testcontainers:postgresql")
}

Full code