Module org.jooq

Class MockConnection

java.lang.Object
org.jooq.tools.jdbc.JDBC41Connection
org.jooq.tools.jdbc.MockConnection
All Implemented Interfaces:
AutoCloseable, Connection, Wrapper

public class MockConnection extends JDBC41Connection implements Connection
A mock connection.

Mock connections can be used to supply jOOQ with unit test data, avoiding the round-trip of using an actual in-memory test database, such as Derby, H2 or HSQLDB. A usage example:

 MockDataProvider provider = new MockDataProvider() {
     public MockResult[] execute(MockExecuteContext context) throws SQLException {
         Result<MyTableRecord> result = executor.newResult(MY_TABLE);
         result.add(executor.newRecord(MY_TABLE));

         return new MockResult[] {
             new MockResult(1, result)
         };
     }
 };
 Connection connection = new MockConnection(provider);
 DSLContext create = DSL.using(connection, dialect);
 assertEquals(1, create.selectOne().fetch().size());
 

While this MockConnection can be used independently of jOOQ, it has been optimised for usage with jOOQ. JDBC features that are not used by jOOQ (e.g. procedure bind value access by parameter name) are not supported in this mock framework

Disclaimer: The general idea of mocking a JDBC connection with this jOOQ API is to provide quick workarounds, injection points, etc. using a very simple JDBC abstraction. It is NOT RECOMMENDED to emulate an entire database (including complex state transitions, transactions, locking, etc.) using this mock API. Once you have this requirement, please consider using an actual database instead for integration testing (e.g. using https://www.testcontainers.org), rather than implementing your test database inside of a MockDataProvider.

Author:
Lukas Eder