1

I am writing a Kotlin JVM application that uses the SQLDelight to generate classes that needs to interact with a MySQL database.

How can I write unit tests for classes/services that use the classes that SQLDelight library generated?

For example, SQLDelight generated interfaces like these:

interface SomeDB : Transacter {
  val someTableQueries: SomeTableQueries

  companion object {
    val Schema: SqlDriver.Schema
      get() = SomeDB::class.schema
    operator fun invoke(driver: SqlDriver): SomeDB = SomeDB::class.newInstance(driver)
  }
}
interface SomeTableQueries : Transacter {
  fun <T : Any> querySomething(name: String, mapper: (
    id: Long,
    name: String,
    address: String
  ) -> T): Query<T>

  fun querySomething(name: String): Query<SomeTable>
}

And somewhere in my application, I have myService.findAllAddressForName(name: String), it will do something like someDB.someTableQueries.querySomething(name).

So what is the best way to unit test myService.findAllAddressForName?

If I need to use libraries like Mockito or mockito-kotlin, which classes should I be mocking? Is there any better way for me to write tests for these? Thanks.

lawkai
  • 155
  • 1
  • 9
  • I have actually found a work around for this from the sqldelight repo. We can actually use testcontainer to achieve this. https://github.com/cashapp/sqldelight/blob/master/sqldelight-gradle-plugin/src/test/integration-mysql-schema/src/test/kotlin/com/squareup/sqldelight/mysql/integration/MySqlTest.kt I will leave this question open in case someone has a better solution to this. – lawkai Jul 29 '20 at 16:35

0 Answers0