@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); }
@Test public void testUpdatePreparedStatementForUpdate() 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.setInt(1, search.getMessageTypeId()); EasyMock.expectLastCall(); preparedStatement.setString(2, search.getQueryType()); EasyMock.expectLastCall(); preparedStatement.setString(3, search.getJsonData()); EasyMock.expectLastCall(); preparedStatement.setInt(4, search.getSearchUserId()); EasyMock.expectLastCall(); preparedStatement.setString(5, search.getSearchName()); EasyMock.expectLastCall(); EasyMock.replay(preparedStatement); dao.updatePreparedStatementForUpdate(preparedStatement, search); EasyMock.verify(preparedStatement); }