예제 #1
0
  @Test
  public void testDelete() throws Exception {
    int id = 34;
    String name = "myName";
    SavedReviewerSearchDAO dao =
        EasyMock.createMockBuilder(SavedReviewerSearchDAO.class)
            .withConstructor()
            .addMockedMethod("delete", 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.delete(mockConn, id, name)).andReturn(true);

    MockExecutionWithThrow.<Boolean>execute(txMgr, mockConn);
    EasyMock.replay(txMgr, mockConn, dao);
    assertTrue("Wrong delete result.", dao.delete(id, name));
    EasyMock.verify(txMgr, mockConn, dao);
  }
예제 #2
0
  @Test
  public void testDeleteWithConnection() throws Exception {
    PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class);
    Connection connection = EasyMock.createStrictMock(Connection.class);
    int id = 34;
    String name = "myName";
    int count = 342;
    SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO();

    EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.DELETE_SQL))
        .andReturn(preparedStatement);
    preparedStatement.setInt(1, id);
    EasyMock.expectLastCall();
    preparedStatement.setString(2, name);
    EasyMock.expectLastCall();
    EasyMock.expect(preparedStatement.executeUpdate()).andReturn(count);
    preparedStatement.close();
    EasyMock.expectLastCall();

    EasyMock.replay(preparedStatement, connection);
    assertTrue("Should return true when item is deleted.", dao.delete(connection, id, name));
    EasyMock.verify(preparedStatement, connection);

    // failure case
    count = 0;
    EasyMock.reset(preparedStatement, connection);
    EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.DELETE_SQL))
        .andReturn(preparedStatement);
    preparedStatement.setInt(1, id);
    EasyMock.expectLastCall();
    preparedStatement.setString(2, name);
    EasyMock.expectLastCall();
    EasyMock.expect(preparedStatement.executeUpdate()).andReturn(count);
    preparedStatement.close();
    EasyMock.expectLastCall();

    EasyMock.replay(preparedStatement, connection);
    assertFalse("Should return false when item is not deleted.", dao.delete(connection, id, name));
    EasyMock.verify(preparedStatement, connection);
  }