@Test public void testResultFromResultSet() throws Exception { SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Date createdDate = new Date(); int customerId = 56; int userId = 32422; int msgTypeId = 2342; String name = "myName"; String queryType = "myQueryType"; String jsonData = "this is my json data but I don't check that it is json."; EasyMock.expect(resultSet.getString(1)).andReturn(name); EasyMock.expect(resultSet.getInt(2)).andReturn(customerId); EasyMock.expect(resultSet.getInt(3)).andReturn(userId); EasyMock.expect(resultSet.getInt(4)).andReturn(msgTypeId); EasyMock.expect(resultSet.getString(5)).andReturn(queryType); EasyMock.expect(resultSet.getString(6)).andReturn(jsonData); EasyMock.expect(resultSet.getTimestamp(7)).andReturn(new Timestamp(createdDate.getTime())); EasyMock.replay(resultSet); SavedReviewerSearch search = dao.resultFromResultSet(resultSet); EasyMock.verify(resultSet); assertEquals("Wrong created date.", createdDate, search.getCreatedDate()); assertEquals("Wrong customer id.", customerId, search.getCustomerId()); assertEquals("Wrong user id.", userId, search.getSearchUserId()); assertEquals("Wrong msg type id.", msgTypeId, search.getMessageTypeId()); assertEquals("Wrong name.", name, search.getSearchName()); assertEquals("Wrong query type.", queryType, search.getQueryType()); assertEquals("Wrong json data.", jsonData, search.getJsonData()); // now with no date EasyMock.reset(resultSet); EasyMock.expect(resultSet.getString(1)).andReturn(name); EasyMock.expect(resultSet.getInt(2)).andReturn(customerId); EasyMock.expect(resultSet.getInt(3)).andReturn(userId); EasyMock.expect(resultSet.getInt(4)).andReturn(msgTypeId); EasyMock.expect(resultSet.getString(5)).andReturn(queryType); EasyMock.expect(resultSet.getString(6)).andReturn(jsonData); EasyMock.expect(resultSet.getTimestamp(7)).andReturn(null); EasyMock.replay(resultSet); search = dao.resultFromResultSet(resultSet); EasyMock.verify(resultSet); assertNull("Created date should not be set.", search.getCreatedDate()); }
@Test public void testReadWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Connection connection = EasyMock.createStrictMock(Connection.class); SavedReviewerSearch search = new SavedReviewerSearch(); int id = 34; String name = "myName"; SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("resultFromResultSet") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.READ_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, id); EasyMock.expectLastCall(); preparedStatement.setString(2, name); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(dao.resultFromResultSet(resultSet)).andReturn(search); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); SavedReviewerSearch result = dao.read(connection, id, name); assertEquals("Wrong search returned", search, result); EasyMock.verify(preparedStatement, resultSet, connection, dao); // failure case EasyMock.reset(preparedStatement, resultSet, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.READ_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, id); EasyMock.expectLastCall(); preparedStatement.setString(2, name); 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.read(connection, id, name); fail("Should have thrown a sql exception."); } catch (SQLException ex) { // expected } EasyMock.verify(preparedStatement, resultSet, connection, dao); }