Search code examples
javajooq

How to construct org.jooq.Result for unit testing


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.


Solution

  • 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