Package org.pjdbc.drivers
Class MockDriver
java.lang.Object
org.pjdbc.sql.AbstractDriver
org.pjdbc.drivers.MockDriver
- All Implemented Interfaces:
Driver
@DriverCapability(prefix="mock",
description="In-memory mock driver for testing",
capabilities="testing",
composable=false,
terminal=true)
@DriverSideEffects(stateful=true)
public class MockDriver
extends AbstractDriver
In-memory mock driver for testing JDBC code without a real database.
Basic Usage
// Get a connection (logs all method calls)
Connection conn = DriverManager.getConnection("jdbc:mock:test");
// Check what was executed
String log = MockDriver.getLog("jdbc:mock:test");
Result Configuration API
Configure what results queries should return:
// Configure a query to return specific data
MockDriver.when("SELECT * FROM users")
.thenReturn(MockResultSet.create()
.columns("id", "name", "email")
.row(1, "Alice", "alice@example.com")
.row(2, "Bob", "bob@example.com")
.build());
// Configure an update count
MockDriver.when("INSERT INTO users").thenUpdate(1);
// Configure an exception
MockDriver.when("DROP TABLE").thenThrow(new SQLException("Not allowed"));
// Pattern matching with regex
MockDriver.whenMatches("SELECT.*FROM users WHERE id = .*")
.thenReturn(MockResultSet.create()
.columns("id", "name")
.row(1, "Alice")
.build());
// Dynamic result based on SQL
MockDriver.whenMatches("SELECT.*FROM users WHERE id = (\\d+)")
.thenAnswer(sql -> {
Matcher m = Pattern.compile("id = (\\d+)").matcher(sql);
if (m.find()) {
int id = Integer.parseInt(m.group(1));
return MockResultSet.create()
.columns("id", "name")
.row(id, "User" + id)
.build();
}
return MockResultSet.empty();
});
// Reset all configurations
MockDriver.reset();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents a configured expectation for SQL execution.static classPrintWriter that exposes its underlying output stream. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanacceptsSubName(String subname) protected booleanacceptsSubProtocol(String subprotocol) static voidstatic voidconnect(String url, Properties info) static Propertiesstatic Stringstatic voidreset()Reset all configured expectations.static MockDriver.ExpectationConfigure an expectation for SQL containing the given string.static MockDriver.ExpectationwhenMatches(String sqlPattern) Configure an expectation for SQL matching the given regex pattern.Methods inherited from class org.pjdbc.sql.AbstractDriver
acceptsProtocol, acceptsURL, getMajorVersion, getMinorVersion, getParentLogger, getPropertyInfo, getUrlParameter, getUrlParameter, getUrlParameters, jdbcCompliant, parseUrl, protocol, subname, subprotocol, validateParameters
-
Constructor Details
-
MockDriver
public MockDriver()
-
-
Method Details
-
when
Configure an expectation for SQL containing the given string.Example:
MockDriver.when("SELECT * FROM users").thenReturn(resultSet);- Parameters:
sqlContains- SQL substring to match (case-insensitive)- Returns:
- an Expectation to configure the result
-
whenMatches
Configure an expectation for SQL matching the given regex pattern.Example:
MockDriver.whenMatches("SELECT.*FROM users WHERE id = \\d+").thenReturn(resultSet);- Parameters:
sqlPattern- regex pattern to match (case-insensitive, DOTALL mode)- Returns:
- an Expectation to configure the result
-
reset
public static void reset()Reset all configured expectations. -
getLastConnectionInfo
-
getLog
-
clearLogs
public static void clearLogs() -
clearLog
-
acceptsSubProtocol
- Specified by:
acceptsSubProtocolin classAbstractDriver
-
acceptsSubName
- Specified by:
acceptsSubNamein classAbstractDriver
-
connect
- Throws:
SQLException
-