-
- All Known Implementing Classes:
MockFileDatabase
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface MockDataProvider
A data provider for mock query executions.Supply this data provider to your
MockConnection
in order to globally provide data for SQL statements.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 (see the
execute(MockExecuteContext)
method). 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, rather than implementing your test database inside of aMockDataProvider
.See
execute(MockExecuteContext)
for more details.- Author:
- Lukas Eder
- See Also:
MockConnection
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description MockResult[]
execute(MockExecuteContext ctx)
Execution callback for a JDBC query execution.
-
-
-
Method Detail
-
execute
MockResult[] execute(MockExecuteContext ctx) throws SQLException
Execution callback for a JDBC query execution.This callback will be called by
MockStatement
upon the various statement execution methods. These include:Statement.execute(String)
Statement.execute(String, int)
Statement.execute(String, int[])
Statement.execute(String, String[])
Statement.executeBatch()
Statement.executeQuery(String)
Statement.executeUpdate(String)
Statement.executeUpdate(String, int)
Statement.executeUpdate(String, int[])
Statement.executeUpdate(String, String[])
PreparedStatement.execute()
PreparedStatement.executeQuery()
PreparedStatement.executeUpdate()
The various execution modes are unified into this simple method. Implementations should adhere to this contract:
MockStatement
does not distinguish between "static" and "prepared" statements. However, a non-emptyMockExecuteContext.bindings()
is a strong indicator for aPreparedStatement
.MockStatement
does not distinguish between "batch" and "single" statements. However...- A
MockExecuteContext.batchSQL()
with more than one SQL string is a strong indicator for a "multi-batch statement", as understood by jOOQ'sDSLContext.batch(Query...)
. - A
MockExecuteContext.batchBindings()
with more than one bind variable array is a strong indicator for a "single-batch statement", as understood by jOOQ'sDSLContext.batch(Query)
.
- A
- It is recommended to return as many
MockResult
objects as batch executions. In other words, you should guarantee that:int multiSize = context.getBatchSQL().length; int singleSize = context.getBatchBindings().length; assertEquals(result.length, Math.max(multiSize, singleSize))
This holds true also for non-batch executions (where both sizes are equal to
1
) - You may also return more than one result for non-batch executions.
This is useful for procedure calls with several result sets.
- In JDBC, such additional result sets can be obtained with
Statement.getMoreResults()
. - In jOOQ, such additional result sets can be obtained with
ResultQuery.fetchMany()
- In JDBC, such additional result sets can be obtained with
- If generated keys (
Statement.RETURN_GENERATED_KEYS
) are requested from this execution, you can also addMockResult.data
to your first result, in addition to the affectedMockResult.rows
. The relevant flag is passed fromMockStatement
to any of these properties: - OUT parameters from stored procedures are generated from the first
MockResult
's firstRecord
. If OUT parameters are requested, implementors must ensure the presence of such aRecord
.
- Parameters:
ctx
- The execution context.- Returns:
- The execution results. If a
null
or an emptyMockResult[]
is returned, this has the same effect as returningnew MockResult[] { new MockResult(-1, null) }
- Throws:
SQLException
- ASQLException
that is passed through to jOOQ.
-
-