Skip to content

Instantly share code, notes, and snippets.

@sombriks
Created May 18, 2024 00:06
Show Gist options
  • Save sombriks/5dee59a54259634e2e0d6d00aa616195 to your computer and use it in GitHub Desktop.
Save sombriks/5dee59a54259634e2e0d6d00aa616195 to your computer and use it in GitHub Desktop.
sample testconfiguration for spring
-- script to create things that are supposed to be there even _before_ liquibase kicks in
create schema myapp;
package sample.example
import org.hamcrest.CoreMatchers
import org.hamcrest.MatcherAssert
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Import
import org.springframework.data.domain.PageRequest
import org.springframework.test.context.ActiveProfiles
import sample.example.config.TCConfiguration
import sample.example.config.JPAConfig
import sample.example.config.JDBCConfig
@SpringBootTest
@ActiveProfiles("test")
@Import(value = [TCConfiguration::class, JPAConfig::class, JDBCConfig::class])
class TCApplicationTests {
@Test
fun contextLoads() {
}
// other data-related tests
}
package sample.example.config
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
import org.springframework.context.annotation.Bean
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.utility.DockerImageName
@TestConfiguration(proxyBeanMethods = false)
class TCConfiguration {
@Bean
@ServiceConnection
fun postgresContainer(): PostgreSQLContainer<*> {
return PostgreSQLContainer(
DockerImageName
.parse("postgres:13-alpine")
)
.withEnv(
mapOf(
"POSTGRES_DB" to "mydatabase",
"POSTGRES_USER" to "postgres",
"POSTGRES_PASSWORD" to "postgres"
)
)
.withInitScript("initial.sql")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment