Skip to content

Instantly share code, notes, and snippets.

@barlog-m
Created July 18, 2018 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barlog-m/cfca31d4f3e96496918081d8a7b8e918 to your computer and use it in GitHub Desktop.
Save barlog-m/cfca31d4f3e96496918081d8a7b8e918 to your computer and use it in GitHub Desktop.
Integration abstract class for Spring Boot and Testcontainers
import bot.config.ITConfig
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.boot.test.util.TestPropertyValues
import org.springframework.context.ConfigurableApplicationContext
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.ApplicationListener
import org.springframework.context.event.ContextStoppedEvent
import org.springframework.test.context.ContextConfiguration
import org.testcontainers.containers.GenericContainer
@ExtendWith(SpringExtension::class)
@SpringBootTest(
classes = [ITConfig::class],
webEnvironment = WebEnvironment.NONE
)
@ActiveProfiles("test")
@ContextConfiguration(initializers = [BaseIntegrationTest.Initializer::class])
abstract class BaseIntegrationTest {
companion object {
init {
System.setProperty("io.netty.noUnsafe", true.toString())
}
private class KGenericContainer(imageName: String) :
GenericContainer<KGenericContainer>(imageName)
private const val MONGO_PORT = 27017
private val mongoContainer: KGenericContainer =
KGenericContainer("mongo:latest")
.withExposedPorts(MONGO_PORT)
}
class Initializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
override fun initialize(context: ConfigurableApplicationContext) {
mongoContainer.start()
TestPropertyValues.of(
"spring.data.mongodb.uri=mongodb://localhost:${mongoContainer.getMappedPort(MONGO_PORT)}")
.applyTo(context)
context.addApplicationListener(ApplicationListener<ContextStoppedEvent> {
mongoContainer.stop()
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment