1

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.

Dimitry
  • 99
  • 6
  • 1
    Why do you want to do that? Anyway the easiest way is to create a repository with a method that contains this select and the mock the repository method – Simon Martinelli Jun 16 '21 at 15:25

1 Answers1

1

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.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509