Hi! Is there any way to simulate a select failure ...
# exposed
c
Hi! Is there any way to simulate a select failure with DSL API during testing?
e
You could use MockK to mock your table to throw
ExposedSQLException
c
Hi Evan! Could you provide an example? Thanks!
e
Assuming you already have a database table such as this (doing this without an IDE, calls may be slightly incorrect):
Copy code
object MyTable : IntIdTable {
  val name = varchar("name", 128)
}
You can use MockK to mock the “select” method, like:
Copy code
mockkObject(MyTable)
every { MyTable.select(any()) } throws ExposedSQLException( /* Add params for exception here */ )

assertThrows(ExposedSQLException) { MyTable.select { MyTable.id eq 1 } }

// Use this to undo mock
unmockkObject(MyTable)
c
I'm trying Mockk and I have the same problem as with Mockito, I get "java.lang.IllegalStateException: No transaction in context.", I suspected that It could happen. Any ideas?
e
Sounds like you may be mocking the wrong method. That exception shouldn’t be thrown if the actual method is mocked
c
I tried with your snippet and It didn't work.
e
Found the issue. Turns out
.select()
is an extension function. The following snippet worked for me:
Copy code
mockkStatic("org.jetbrains.exposed.sql.QueriesKt")
every { MyTable.select(any<Op<Boolean>>()) } throws mockk<ExposedSQLException>()

assertThrows<ExposedSQLException> { MyTable.select { MyTable.id eq 5 } }
Extension functions aren’t actually part of the object itself, they are compiled to static methods on a synthetic class created for the file. To mock it you need to perform a static mock of the file containing the extension function, in this case QueriesKt (from
org.jetbrains.exposed.sql.Queries.kt
)
c
Great!! Thanks Evan! It works
e
Glad to hear it!