@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 testUpdate() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("update", Connection.class, SavedReviewerSearch.class) .createStrictMock(); ITransactionManager txMgr = EasyMock.createStrictMock(ITransactionManager.class); dao.setTransactionManager(txMgr); Connection mockConn = EasyMock.createStrictMock(Connection.class); EasyMock.expect(dao.update(mockConn, search)).andReturn(true); MockExecutionWithThrow.<Boolean>execute(txMgr, mockConn); EasyMock.replay(txMgr, mockConn, dao); assertEquals("Wrong search result.", true, dao.update(search)); EasyMock.verify(txMgr, mockConn, dao); }
@Test public void testUpdateWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); Connection connection = EasyMock.createStrictMock(Connection.class); SavedReviewerSearch search = new SavedReviewerSearch(); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("updatePreparedStatementForUpdate") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.UPDATE_SQL)) .andReturn(preparedStatement); dao.updatePreparedStatementForUpdate(preparedStatement, search); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeUpdate()).andReturn(1); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, connection, dao); assertEquals("Wrong result returned.", true, dao.update(connection, search)); EasyMock.verify(preparedStatement, connection, dao); // failure case EasyMock.reset(preparedStatement, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.UPDATE_SQL)) .andReturn(preparedStatement); dao.updatePreparedStatementForUpdate(preparedStatement, search); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeUpdate()).andThrow(new SQLException("blah")); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, connection, dao); try { dao.update(connection, search); fail("Should have thrown a sql exception."); } catch (SQLException ex) { // expected } EasyMock.verify(preparedStatement, connection, dao); }