MockRunnerのJDBC Mockを使ってみた


S2UnitDB使ExcelDBExcelDBExcel鹿
DBJDBCSQLSQLMockRunnerJDBC Mock使


http://mockrunner.sourceforge.net/examplesjdbc.html


S2JDBC使S2UnitJDBC Mock使BasicJDBCTestCaseAdapterJDBCTestModuleJDBCMockObjectFactorysetUp()tearDown()
public class UserServiceTest extends S2TestCase {

  private UserService userService;

  private JDBCTestModule jdbcTestModule;
  private JDBCMockObjectFactory jdbcMockObjectFactory;

  protected void setUp() throws Exception {
    super.setUp();
    jdbcMockObjectFactory = new JDBCMockObjectFactory();
    jdbcTestModule = new JDBCTestModule(jdbcMockObjectFactory);
    include("app.dicon");
  }

  protected void tearDown() throws Exception {
    super.tearDown();
    jdbcMockObjectFactory.restoreDrivers();
    jdbcTestModule = null;
    jdbcMockObjectFactory = null;
  }

  public void testGetUserInfoTx() {
    MockConnection connection = jdbcMockObjectFactory.getMockConnection();
    PreparedStatementResultSetHandler statementHandler 
      = connection.getPreparedStatementResultSetHandler();
    MockResultSet result = statementHandler.createResultSet();
    result.addRow(new Object[]{"takezoe", "password"});
    statementHandler.prepareGlobalResultSet(result);

    UserInfo userInfo = userService.getUserInfo("takezoe", "password");

    String expectSql = "select " +
      "T1_.USER_ID as C1_," +
      " T1_.PASSWORD as C2_ " +
      "from USER_INFO T1_ " +
      "where ((T1_.USER_ID = ?) and (T1_.PASSWORD = ?))";

    jdbcTestModule.verifySQLStatementExecuted(expectSql);
    jdbcTestModule.verifySQLStatementParameter(expectSql, 0, 1, "takezoe");
    jdbcTestModule.verifySQLStatementParameter(expectSql, 0, 2, "password");
    
    assertNotNull(userInfo);
    assertEquals("takezoe", userInfo.userId);
    assertEquals("password", userInfo.password);
  }

}

JDBC使