@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 testIsUpdate() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); search.setSearchUserId(34); search.setSearchName("hello"); SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); Connection connection = EasyMock.createStrictMock(Connection.class); PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CHECK_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, search.getSearchUserId()); EasyMock.expectLastCall(); preparedStatement.setString(2, search.getSearchName()); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.expectLastCall(); EasyMock.replay(connection, preparedStatement, resultSet); assertTrue("Should be an update.", dao.isUpdate(connection, search)); EasyMock.verify(connection, preparedStatement, resultSet); // now not an update EasyMock.reset(connection, preparedStatement, resultSet); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CHECK_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, search.getSearchUserId()); EasyMock.expectLastCall(); preparedStatement.setString(2, search.getSearchName()); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(false); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.expectLastCall(); EasyMock.replay(connection, preparedStatement, resultSet); assertFalse("Should not be an update.", dao.isUpdate(connection, search)); EasyMock.verify(connection, preparedStatement, resultSet); }