Skip to content
Advertisement

jOOQ: Mocking DAO objects

jOOQ 3.5.0

I’m currently trying to write unit tests for a resource that is using jOOQs generated DAO objects.

I’ve noticed one of the base classes (DAOImpl) in the DAO hierarchy has many final methods which makes it unfriendly to mock (I’m excluding byte code manipulators like Powermock as a solution). I’m currently using the MockConnection and MockDataProvider pattern to fill in my DAOs but it seems like this is a little low level for asserting DAO method calls.

As an example, my resource is calling FooDao.createFoo(foo) and my point of interception in the test is MockDataProvider.execute(...) which provides me with a context object with raw SQL and a binding object array of values.

In order for the test to assert that create has been called, I would need to evaluate the raw sql. Seems wasteful to be doing something like this when the DAO has nice, fluent methods that could be asserted on.

So my question is: is there a better way to unit test DAO usage? Seems like I need A LOT of boiler plate in order to test one simple contract….

FooDao fooDao = createMock(FooDao.class);
....
when(fooDao.fetchById(id)).thenReturn(foo);

is much more preferable to the example in the docs.

Advertisement

Answer

Right now, it will be hard to mock jOOQ DAOs without using something like PowerMock that removes final from bytecode for you in order to override those methods.

In a future jOOQ version, we might implement interface code generation for DAOs, however. Those would be easier to mock.

See also: https://github.com/jOOQ/jOOQ/issues/3868

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement