예제 #1
0
  @Test
  public void testReadWithConnection() throws Exception {
    PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class);
    ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class);
    Connection connection = EasyMock.createStrictMock(Connection.class);
    SavedReviewerSearch search = new SavedReviewerSearch();
    int id = 34;
    String name = "myName";
    SavedReviewerSearchDAO dao =
        EasyMock.createMockBuilder(SavedReviewerSearchDAO.class)
            .withConstructor()
            .addMockedMethod("resultFromResultSet")
            .createStrictMock();

    EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.READ_SQL))
        .andReturn(preparedStatement);
    preparedStatement.setInt(1, id);
    EasyMock.expectLastCall();
    preparedStatement.setString(2, name);
    EasyMock.expectLastCall();
    EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet);
    EasyMock.expect(resultSet.next()).andReturn(true);
    EasyMock.expect(dao.resultFromResultSet(resultSet)).andReturn(search);
    resultSet.close();
    EasyMock.expectLastCall();
    preparedStatement.close();
    EasyMock.replay(preparedStatement, resultSet, connection, dao);
    SavedReviewerSearch result = dao.read(connection, id, name);
    assertEquals("Wrong search returned", search, result);
    EasyMock.verify(preparedStatement, resultSet, connection, dao);

    // failure case
    EasyMock.reset(preparedStatement, resultSet, connection, dao);
    EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.READ_SQL))
        .andReturn(preparedStatement);
    preparedStatement.setInt(1, id);
    EasyMock.expectLastCall();
    preparedStatement.setString(2, name);
    EasyMock.expectLastCall();
    EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet);
    EasyMock.expect(resultSet.next()).andReturn(false);
    resultSet.close();
    EasyMock.expectLastCall();
    preparedStatement.close();
    EasyMock.replay(preparedStatement, resultSet, connection, dao);
    try {
      dao.read(connection, id, name);
      fail("Should have thrown a sql exception.");
    } catch (SQLException ex) {
      // expected
    }
    EasyMock.verify(preparedStatement, resultSet, connection, dao);
  }
예제 #2
0
  @Test
  public void testRead() throws Exception {
    SavedReviewerSearch search = new SavedReviewerSearch();

    int id = 34;
    String name = "mySearch";
    SavedReviewerSearchDAO dao =
        EasyMock.createMockBuilder(SavedReviewerSearchDAO.class)
            .withConstructor()
            .addMockedMethod("read", Connection.class, Integer.TYPE, String.class)
            .createStrictMock();
    ITransactionManager txMgr = EasyMock.createStrictMock(ITransactionManager.class);
    dao.setTransactionManager(txMgr);
    Connection mockConn = EasyMock.createStrictMock(Connection.class);
    EasyMock.expect(dao.read(mockConn, id, name)).andReturn(search);

    MockExecutionWithThrow.<Boolean>execute(txMgr, mockConn);
    EasyMock.replay(txMgr, mockConn, dao);
    assertEquals("Wrong search item returned.", search, dao.read(id, name));
    EasyMock.verify(txMgr, mockConn, dao);
  }