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();
 
  • Constructor Details

    • MockDriver

      public MockDriver()
  • Method Details

    • when

      public static MockDriver.Expectation when(String sqlContains)
      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

      public static MockDriver.Expectation whenMatches(String sqlPattern)
      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

      public static Properties getLastConnectionInfo(String url)
    • getLog

      public static String getLog(String url)
    • clearLogs

      public static void clearLogs()
    • clearLog

      public static void clearLog(String url)
    • acceptsSubProtocol

      protected boolean acceptsSubProtocol(String subprotocol)
      Specified by:
      acceptsSubProtocol in class AbstractDriver
    • acceptsSubName

      protected boolean acceptsSubName(String subname)
      Specified by:
      acceptsSubName in class AbstractDriver
    • connect

      public Connection connect(String url, Properties info) throws SQLException
      Throws:
      SQLException