Last active
November 30, 2020 02:08
-
-
Save arnabmitra/c612570eda353915db7c9caf796546d5 to your computer and use it in GitHub Desktop.
TestContainers Integration tests in a Spring boot Kotlin 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
import com.zaxxer.hikari.HikariConfig | |
import com.zaxxer.hikari.HikariDataSource | |
import org.junit.ClassRule | |
import org.junit.experimental.categories.Category | |
import org.junit.rules.ExternalResource | |
import org.junit.runner.RunWith | |
import org.springframework.boot.test.context.SpringBootTest | |
import org.springframework.boot.test.context.TestConfiguration | |
import org.springframework.context.annotation.Bean | |
import org.springframework.test.context.junit4.SpringRunner | |
import org.testcontainers.containers.GenericContainer | |
import org.testcontainers.containers.PostgreSQLContainer | |
import org.testcontainers.containers.wait.LogMessageWaitStrategy | |
import java.sql.SQLException | |
import java.time.Duration | |
import java.time.temporal.ChronoUnit.SECONDS | |
import javax.sql.DataSource | |
@RunWith(SpringRunner::class) | |
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | |
@Category(IntegrationTest::class) | |
class AccountControllerTest : AccountControllerTestLocal() { | |
companion object { | |
var postgresSQLContainer: KPostgresContainer = KPostgresContainer("yyy.xxx.com/xxx-postgres:9.5.9") | |
.withDatabaseName("xxx_ttt") | |
.withUsername("xxx_sss") | |
.withPassword("xxx") | |
.withExposedPorts(5432).waitingFor(LogMessageWaitStrategy() | |
.withRegEx(".*database system is ready to accept connections.*\\s") | |
.withTimes(2) | |
.withStartupTimeout(Duration.of(60, SECONDS))) | |
@ClassRule | |
@JvmField | |
val resource: ExternalResource = object : ExternalResource() { | |
override fun before() { | |
println("ClassRule Before") | |
this@Companion.postgresSQLContainer.start() | |
Thread.sleep(10000) | |
val genericContainer: KGenericContainer = KGenericContainer("registry.xxx.com/xxx-db:ddl-changes-fff-db") | |
.withEnv("DB_HOST", postgresSQLContainer.containerIpAddress) | |
.withNetworkMode("host") | |
.withEnv("DB_PORT", postgresSQLContainer.getMappedPort(5432).toString()) | |
.withEnv("DB_USER", "xxx_yyy") | |
.withEnv("DB_PASSWORD", "xxx") | |
.withEnv("JAVA_OPTS", "-Xmx64m") | |
.withEnv("PGPASSWORD", "xxx") | |
.withEnv("DB_NAME", "xxx_yyy") | |
.withEnv("POSTGRES_PASSWORD", "xxx") | |
.withEnv("APP_NAME", "zzz-db") | |
.withEnv("DB_DROP", "1") | |
.withEnv("DB_LOAD", "1") | |
.withEnv("DB_ROOT_USER", "xxx_zzz") | |
.withEnv("DB_SERVICE", "postgres") | |
.withEnv("DB_INIT", "1") | |
genericContainer.start() | |
} | |
override fun after() { | |
println("ClassRule After") | |
} | |
} | |
} | |
class KPostgresContainer(imageName: String) : PostgreSQLContainer<KPostgresContainer>(imageName) | |
class KGenericContainer(imageName: String) : GenericContainer<KGenericContainer>(imageName) | |
@TestConfiguration | |
class TestConfig { | |
@Bean | |
@Throws(SQLException::class) | |
fun dataSource(): DataSource { | |
var hikariConfig = HikariConfig() | |
val portMapping = AccountControllerTest.Companion.postgresSQLContainer.getMappedPort(5432) | |
hikariConfig.jdbcUrl = "jdbc:postgresql://localhost:${portMapping}/xxx_zzz" | |
hikariConfig.username = "xxx_zzz" | |
hikariConfig.password = "xxx" | |
hikariConfig.isAllowPoolSuspension = true | |
val ds = HikariDataSource(hikariConfig); | |
return ds | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment