hey guys! is it any tutorial how to use integratio...
# ktor
t
hey guys! is it any tutorial how to use integration tests with test containers in ktor?
a
Do you mean the Testcontainers library?
t
yes exactly
a
Could you please explain in more detail what do you want to test?
I assume you have a server application in a container and you want to test it locally with a HTTP client.
t
No, U just started to learn ktor, created simple crud with exposed and want to test repository layer with testContainers(postgres) in the internet I found that there is a kotest framework provides an opportunity to work with testContainers. Should I use
Copy code
testImplementation("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 testing
a
I suggest asking your questions in the #exposed channel too.
k
I do this in Climate Changemakers Act with SQLDelight. The configuration will probably be slightly different, but this should be helpful. https://github.com/climatechangemakers/climate-changemakers-act/blob/trunk/backend[…]g/climatechangemakers/act/feature/util/TestContainerProvider.kt
t
so the configuration that I am using now looks like
Copy code
class 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)