Search code examples
kotlinmockingjooq

Jooq mock fetchInto


As i found it is possible to mock result of jooq request . But is it possible to mock fetchInto function for example?

I have this code

val addressSaved = dsl.selectFrom(address)
        .where(address.CITY.eq(city))
        .fetchOneInto(Address::class.java)

And I want to mock just fetchOneInto. It will make testing more easily I think.


Solution

  • How to mock xyzInto(Class<E>) methods

    You can replace the implementation of all the xyzInto(Class<E>) style methods using a single SPI: The RecordMapperProvider as documented here: https://www.jooq.org/doc/latest/manual/sql-execution/fetching/pojos-with-recordmapper-provider/

    That way, irrespective of your specific query, you can always populate custom implementations of your Address class (or even some other subclass). You'll still need to mock the statement itself, though. Probably again using a MockConnection as you already discovered.

    Mocking vs integration testing

    You've probably made an informed decision regarding mocking vs. integration testing already, but for future visitors of this question, I'm still mentioning this:

    In general, it is a lot easier and more useful to just integration test your code with your actual database (e.g. using testcontainers), and if you must mock things to test higher level logic, then mock your services, not jOOQ SQL statements.