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()
}
}
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