Timeout on unit test with TransactionalOperator (Kotlin coroutines)

Viewed 284

I have a transactional scope like follows (Spring docs) and I get it working:

class PersonRepository(private val operator: TransactionalOperator) {

    suspend fun initDatabase() = operator.executeAndAwait {
        insertPerson1()
        insertPerson2()
    }
 }

But when I try to mock TransactionalOperator in my test like this:

val transactionalOperator = mockk<TransactionalOperator>(relaxed = true)

I get a timeout every time my test needs the methods that use the operator. Also I haven't found documentation about it.

Does someone have an example of working with tests and mocks on programmatic variant of the Reactive transaction management?

Thanks!

1 Answers

Hope it helps, I used the following construct:

val reactiveTransactionManager = mockk<ReactiveTransactionManager>()
val transactionalOperator = TransactionalOperator.create(reactiveTransactionManager)

every { reactiveTransactionManager.getReactiveTransaction(any()) } answers { mono { mockk() } }
every { reactiveTransactionManager.commit(any()) } answers { mono { mockk() } }
every { reactiveTransactionManager.rollback(any()) } answers { mono { mockk() } }
Related