@Test public void testFindByUserId() throws Exception { List<String> results = new LinkedList<String>(); SavedReviewerSearchQuery constraints = new SavedReviewerSearchQuery(); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("find", Connection.class, SavedReviewerSearchQuery.class) .createStrictMock(); ITransactionManager txMgr = EasyMock.createStrictMock(ITransactionManager.class); dao.setTransactionManager(txMgr); Connection mockConn = EasyMock.createStrictMock(Connection.class); EasyMock.expect(dao.find(mockConn, constraints)).andReturn(results); MockExecutionWithThrow.<Boolean>execute(txMgr, mockConn); EasyMock.replay(txMgr, mockConn, dao); assertEquals("Wrong searches returned.", results, dao.find(constraints)); EasyMock.verify(txMgr, mockConn, dao); }
@Test public void testFinddWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Connection connection = EasyMock.createStrictMock(Connection.class); String search1 = "search1"; String search2 = "search2"; int userId = 34; int messageTypeId = 1423; String queryType = "myQueryType"; SavedReviewerSearchQuery constraints = new SavedReviewerSearchQuery(); constraints.setQueryType(queryType); constraints.setMessageTypeId(messageTypeId); constraints.setSearchUserId(userId); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("resultFromResultSet") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.FIND_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, userId); EasyMock.expectLastCall(); preparedStatement.setInt(2, messageTypeId); EasyMock.expectLastCall(); preparedStatement.setString(3, queryType); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getString(1)).andReturn(search1); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getString(1)).andReturn(search2); EasyMock.expect(resultSet.next()).andReturn(false); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); List<String> results = dao.find(connection, constraints); EasyMock.verify(preparedStatement, resultSet, connection, dao); assertEquals("Wrong # of items in list.", 2, results.size()); assertTrue("Did not find result 1 in list.", results.contains(search1)); assertTrue("Did not find result 2 in list.", results.contains(search2)); // no items EasyMock.reset(preparedStatement, resultSet, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.FIND_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, userId); EasyMock.expectLastCall(); preparedStatement.setInt(2, messageTypeId); EasyMock.expectLastCall(); preparedStatement.setString(3, queryType); 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); results = dao.find(connection, constraints); EasyMock.verify(preparedStatement, resultSet, connection, dao); assertEquals("Wrong # of items in list.", 0, results.size()); }