@Test public void testCreateWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Connection connection = EasyMock.createStrictMock(Connection.class); SavedReviewerSearch search = new SavedReviewerSearch(); Date date = new Date(); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("updatePreparedStatementForCreate") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CREATE_SQL)) .andReturn(preparedStatement); dao.updatePreparedStatementForCreate(preparedStatement, search); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getTimestamp(1)).andReturn(new Timestamp(date.getTime())); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); SavedReviewerSearch result = dao.create(connection, search); EasyMock.verify(preparedStatement, resultSet, connection, dao); assertEquals("Wrong created date.", date, result.getCreatedDate()); // failure case EasyMock.reset(preparedStatement, resultSet, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CREATE_SQL)) .andReturn(preparedStatement); dao.updatePreparedStatementForCreate(preparedStatement, search); 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.create(connection, search); fail("Should have thrown a sql exception."); } catch (SQLException ex) { // expected } EasyMock.verify(preparedStatement, resultSet, connection, dao); }
@Test public void testUpsertWithConnection() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); SavedReviewerSearch newSearch = new SavedReviewerSearch(); Connection connection = EasyMock.createStrictMock(Connection.class); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("update", Connection.class, SavedReviewerSearch.class) .addMockedMethod("create", Connection.class, SavedReviewerSearch.class) .addMockedMethod("isUpdate") .createStrictMock(); EasyMock.expect(dao.isUpdate(connection, search)).andReturn(false); EasyMock.expect(dao.create(connection, search)).andReturn(newSearch); EasyMock.replay(dao); assertEquals( "Wrong search returned in create case.", newSearch, dao.upsert(connection, search)); EasyMock.verify(dao); EasyMock.reset(dao); EasyMock.expect(dao.isUpdate(connection, search)).andReturn(true); EasyMock.expect(dao.update(connection, search)).andReturn(true); EasyMock.replay(dao); assertEquals("Wrong search returned in update case.", search, dao.upsert(connection, search)); EasyMock.verify(dao); }
@Test public void testCreate() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); SavedReviewerSearch search2 = new SavedReviewerSearch(); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("create", Connection.class, SavedReviewerSearch.class) .createStrictMock(); ITransactionManager txMgr = EasyMock.createStrictMock(ITransactionManager.class); dao.setTransactionManager(txMgr); Connection mockConn = EasyMock.createStrictMock(Connection.class); EasyMock.expect(dao.create(mockConn, search)).andReturn(search2); MockExecutionWithThrow.<Boolean>execute(txMgr, mockConn); EasyMock.replay(txMgr, mockConn, dao); assertEquals("Wrong search item returned.", search2, dao.create(search)); EasyMock.verify(txMgr, mockConn, dao); }