@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 testUpdatePreparedStatementForCreate() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); 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."; search.setCustomerId(customerId); search.setSearchUserId(userId); search.setMessageTypeId(msgTypeId); search.setSearchName(name); search.setQueryType(queryType); search.setJsonData(jsonData); SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); preparedStatement.setString(1, search.getSearchName()); EasyMock.expectLastCall(); preparedStatement.setInt(2, search.getCustomerId()); EasyMock.expectLastCall(); preparedStatement.setInt(3, search.getSearchUserId()); EasyMock.expectLastCall(); preparedStatement.setInt(4, search.getMessageTypeId()); EasyMock.expectLastCall(); preparedStatement.setString(5, search.getQueryType()); EasyMock.expectLastCall(); preparedStatement.setString(6, search.getJsonData()); EasyMock.expectLastCall(); EasyMock.replay(preparedStatement); dao.updatePreparedStatementForCreate(preparedStatement, search); EasyMock.verify(preparedStatement); }