Created
June 19, 2020 15:34
-
-
Save k0staa/53e3c8a887728111c6ca845cfbb1d628 to your computer and use it in GitHub Desktop.
Kotlin mocking transaction. Mockk, Kotest, Exposed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component | |
| class SomeTransactionalService( | |
| private val someDao: SomeDao | |
| ) { | |
| fun handle(parameter: String) { | |
| transaction { | |
| eventDao.updateEvent(parameter) | |
| } | |
| } | |
| } | |
| internal class TransactionServiceTest : BehaviorSpec() { | |
| private val someDao: SomeDao = | |
| mockk(relaxed = true) | |
| private val sut: SomeTransactionalService = | |
| SomeTransactionalService( | |
| someDao | |
| ) | |
| init { | |
| val slot = slot<Transaction.() -> Any>() | |
| mockkStatic("org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt") | |
| every { transaction(any<Database>(), capture(slot)) } answers { | |
| slot.invoke(mockk()) | |
| } | |
| given("parameter") { | |
| val parameter = "param" | |
| `when`("handling param") { | |
| sut.handle(parameter) | |
| then("someDao should be invoked") { | |
| verify { | |
| someDao.updateEvent( | |
| parameter | |
| ) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment