Timur Atakishiev
11/11/2021, 8:23 AMAleksei Tirman [JB]
11/11/2021, 10:18 AMTimur Atakishiev
11/11/2021, 11:42 AMAleksei Tirman [JB]
11/11/2021, 2:37 PMTimur Atakishiev
11/11/2021, 2:43 PMtestImplementation("org.jetbrains.kotlin:kotlin-test:$kotlin_version")
this guy to make everything work, or it is not enough to deal with testContainers, hard to tell. I was looking for a tutorials, could not find anything. All tutorials are without testingAleksei Tirman [JB]
11/12/2021, 7:29 AMkevin.cianfarini
11/19/2021, 3:47 PMTimur Atakishiev
11/24/2021, 9:48 AMclass UserRepositoryTests {
private val container = ContainerInitiator.startContainer()
@Test
fun `should create User`() {
withTestApplication(
{
initiateEnvironmentVariables(container)
configureDatabase()
}
) {
val repository = UserRepositoryImpl()
val user = User(randomUUID(), "John")
repository.insert(user)
val allUsers = repository.findAll()
assertTrue(allUsers.isNotEmpty())
assertTrue {
allUsers.isNotEmpty()
allUsers.contains(user)
allUsers.size == 1
allUsers.first() == user
}
}
}
}
fun Application.initiateEnvironmentVariables(container: Postgres13Container) {
(environment.config as MapApplicationConfig).apply {
put("db.jdbcUrl", container.jdbcUrl)
put("db.username", container.username)
put("db.password", container.password)
}
}
object Postgres13Container : PostgreSQLContainer<Postgres13Container>("postgres:13-alpine")
object ContainerInitiator {
fun startContainer(): Postgres13Container {
return Postgres13Container
.withDatabaseName("ktor_init")
.withUsername("postgres")
.withPassword("postgres")
.also { it.start() }
}
}
And I dont know whether it is valid or not, it works, but is it the right way..who knows)