Reputation: 91
I have a problem mocking an empty result with jOOQ (version 3.12.3
). I use MockDataProvider
, but it still returns one row.
Here is the code of MockDataProvider
:
class DataProvider implements MockDataProvider {
@Override
public MockResult[] execute(MockExecuteContext ctx) {
return new MockResult[]{new MockResult(0, null)};
}
}
var dbContext = DSL.using(new MockConnection(new DataProvider()));
//....
So it should return 0 rows, and that's what is needed. However, it actually returns 1 row with 0
as value.
12:51:34.799 [main] DEBUG org.jooq.tools.LoggerListener - Affected row(s) : 0
12:51:34.863 [main] DEBUG org.jooq.tools.LoggerListener - Fetched result : +--------+
12:51:34.863 [main] DEBUG org.jooq.tools.LoggerListener - : |metadata|
12:51:34.863 [main] DEBUG org.jooq.tools.LoggerListener - : +--------+
12:51:34.863 [main] DEBUG org.jooq.tools.LoggerListener - : |0 |
12:51:34.863 [main] DEBUG org.jooq.tools.LoggerListener - : +--------+
12:51:34.863 [main] DEBUG org.jooq.tools.LoggerListener - Fetched row(s) : 1
Any help or advice is very appreciated.
Upvotes: 2
Views: 608
Reputation: 221125
Your MockResult
instance encodes an update count of 0
for use with DML statements, not a result set with 0
rows. To achieve that, you will have to create:
new MockResult(0, result)
Or alternatively
Mock.of(result)
With
// some mock DSLContext configuration, as simple as DSL.using(SQLDialect.H2)
DSLContext ctx = ...
Result<Record> result = ctx.newResult(field1, field2);
In a lot of cases, even if the result set is empty, jOOQ will need to know the column names and types of your result set. Those are present as well on any ordinary empty SQL result set.
Upvotes: 3