Skip to content

turbomates/kotlin-test

Repository files navigation

Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. Build Detekt

Test Support

Provides infrastructure for Ktor + Exposed (mostly DAO option) based applications for creating elegant tests Support for:

  • Database (Exposed)
  • Request building
  • Response utils and assertions
@Test
fun `test`() = integrationTest {
        val user = UserMother.hasUser().build()

        handleGet("/api/users/${user.id}") {
        }.assert { // HTTP 200 is checked here
            mapTo<UserView>() shouldBe UserView()
        }
    }

Test database usage

Either create testDatabase property with your DBMS credentials or just change the Config

internal val testDatabase by lazy {
    Database.connect(
        Config.databaseUrl,
        user = Config.user,
        password = Config.password,
        driver = Config.driver,
        databaseConfig = DatabaseConfig { useNestedTransactions = true },
        manager = { database ->
            TransactionManager(
                database,
                Connection.TRANSACTION_READ_COMMITTED,
                DEFAULT_REPETITION_ATTEMPTS
            )
        }
    )
}

Use rollbackTransaction lambda in you tests

fun <T> rollbackTransaction(db: Database = testDatabase, statement: Transaction.() -> T): T {
    return transaction(db) { val result = statement(); rollback(); result }
}

You can find examples in the tests of this project