Reputation: 80
I am writing unit tests for fetching records from Oracle DB using JOOQ library and I need to mock data returned by DSLContext
's fetch()
function. How can I create sample Result<Record>
to be returned by mocked function? I googled it for few hours and could not find an answer.
Upvotes: 2
Views: 1274
Reputation: 1852
I've faced the same issue, and I didn't want to have a MockDataProvider
, as I was testing something else than the DAO. Therefore, I created a trivial function in order to convert a Record
(or multiple) into a Result<T>
.
Note that this is in Kotlin, but it should be easy to translate this into Java:
val jooq = DSL.using(SQLDialect.POSTGRES)
fun <T : Record> result(table: TableImpl<T>, vararg data: T): Result<T> {
return jooq.newResult(table).apply { addAll(data) }
}
Which then can be used as follows:
result(TABLE_NAME, <a record>, <another record>)
And records can just be created using their constructors.
Upvotes: 1
Reputation: 444
Try to use JOOQ's own mock API. Here are the official docs
You probably want to end up with something like that:
final MockDataProvider myMockProvider = new MockDataProvider() {
@Override
public MockResult[] execute(final MockExecuteContext context) throws SQLException {
final DSLContext context = DSL.using(SQLDialect.ORACLE);
final Result<Record> resultRecord = context.newResult(YOUR_TABLE_HERE);
// customize your record with needed fields
resultRecord.add(context.newRecord(YOUR_TABLE_HERE));
return new MockResult[] { new MockResult(1, resultRecord) };
}
};
final DSLContext mockedDSL = DSL.using(new MockConnection(myMockProvider), SQLDialect.ORACLE);
// here you go with your tests
Upvotes: 2