https://kotlinlang.org logo
#ktor
Title
# ktor
t

Timur Atakishiev

11/11/2021, 8:23 AM
hey guys! is it any tutorial how to use integration tests with test containers in ktor?
a

Aleksei Tirman [JB]

11/11/2021, 10:18 AM
Do you mean the Testcontainers library?
t

Timur Atakishiev

11/11/2021, 11:42 AM
yes exactly
a

Aleksei Tirman [JB]

11/11/2021, 2:37 PM
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

Timur Atakishiev

11/11/2021, 2:43 PM
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

Aleksei Tirman [JB]

11/12/2021, 7:29 AM
I suggest asking your questions in the #exposed channel too.
k

kevin.cianfarini

11/19/2021, 3:47 PM
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

Timur Atakishiev

11/24/2021, 9:48 AM
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)
3 Views