I’m trying to unit test some functions that contai...
# exposed
c
I’m trying to unit test some functions that contain
transaction {}
blocks from Exposed, but I keep running into an exception saying that no default database is configured. I don’t want to spin up H2 or any in-memory database just for these tests, since that feels like overkill for simple unit testing. In older versions of Exposed, I remember being able to extend
TransactionManager
to work around this, but it’s no longer an open class. I’m a bit surprised there isn’t a built-in way to mock or bypass transactions for unit tests. What’s the recommended approach for handling this?
For now I have created an abstraction for transaction blocks
Copy code
interface Transactional {
    fun <T> execute(block: () -> T): T
}

class ExposedTransactional : Transactional {
    override fun <T> execute(block: () -> T): T = transaction { block() }
}

class TestTransactional : Transactional {
    override fun <T> execute(block: () -> T): T = block()
}
then services that have to wrap multiple repository calls in a transaction will use the abstraction.
c
Hi @Corey If it's not possible to isolate the logic inside the transaction block for unit testing, since it appears an active transaction or even database connection isn't needed, then you should still be able to mock the JDBC/R2DBC transaction manager. The current solution is not the most elegant and is on our roadmap to eventually smoothen out, but it should work. Here is a full mock example in our tests.