Utils { static void logErrorMessageWithThrowableWithCounter(Logger logger, AtomicInteger atomicInteger, String errorMessage, Throwable throwable) { logErrorMessageWithThrowableWithCounter(logger, atomicInteger, errorMessage, throwable, WAIT_COUNT_MAX); } private  Utils();   }
@Test public void testLogErrorMsgAndThrowableWaitDefault() throws Exception { final String expectedErrorMessage = "This is a test error message!"; final Exception expectedException = new Exception("test exception"); EasyMockSupport mockSupport = new EasyMockSupport(); Logger loggerMock = mockSupport.createMock(Logger.class); AtomicInteger testAtomicInteger = new AtomicInteger(0); Capture<Exception> exceptionCaptureOne = EasyMock.newCapture(); loggerMock.error(eq(expectedErrorMessage), capture(exceptionCaptureOne)); expectLastCall().times(1); mockSupport.replayAll(); for (int i = 0; i < 1000; i++) { Utils.logErrorMessageWithThrowableWithCounter(loggerMock, testAtomicInteger, expectedErrorMessage, expectedException); } assertSame("Exception passed to Logger should have been the same instance passed into the Utils method", expectedException, exceptionCaptureOne.getValue()); mockSupport.verifyAll(); }
@Test public void testLogErrorMsgAndThrowableWaitDefaultExceedsMaxCount() throws Exception { final String expectedErrorMessage = "This is a test error message that should only repeat once!"; final Exception expectedException = new Exception("test exception"); EasyMockSupport mockSupport = new EasyMockSupport(); Logger loggerMock = mockSupport.createMock(Logger.class); AtomicInteger testAtomicInteger = new AtomicInteger(0); Capture<Exception> exceptionCaptureOne = EasyMock.newCapture(); Capture<Exception> exceptionCaptureTwo = EasyMock.newCapture(); loggerMock.error(eq(expectedErrorMessage), capture(exceptionCaptureOne)); loggerMock.error(eq(expectedErrorMessage), capture(exceptionCaptureTwo)); mockSupport.replayAll(); for (int i = 0; i < 2000; i++) { Utils.logErrorMessageWithThrowableWithCounter(loggerMock, testAtomicInteger, expectedErrorMessage, expectedException); } assertSame("Exception passed to Logger should have been the same instance passed into the Utils method", expectedException, exceptionCaptureOne.getValue()); assertSame("Exception passed to Logger should have been the same instance passed into the Utils method", expectedException, exceptionCaptureTwo.getValue()); mockSupport.verifyAll(); }
@Test public void testLogErrorMsgAndThrowableWithCustomWaitMax() throws Exception { final String expectedErrorMessage = "This is a test error message!"; final Exception expectedException = new Exception("test exception"); EasyMockSupport mockSupport = new EasyMockSupport(); Logger loggerMock = mockSupport.createMock(Logger.class); AtomicInteger testAtomicInteger = new AtomicInteger(0); Capture<Exception> exceptionCaptureOne = EasyMock.newCapture(); loggerMock.error(eq(expectedErrorMessage), capture(exceptionCaptureOne)); expectLastCall().times(1); mockSupport.replayAll(); for (int i = 0; i < 5; i++) { Utils.logErrorMessageWithThrowableWithCounter(loggerMock, testAtomicInteger, expectedErrorMessage, expectedException, 5); } assertSame("Exception passed to Logger should have been the same instance passed into the Utils method", expectedException, exceptionCaptureOne.getValue()); mockSupport.verifyAll(); }
@Test public void testLogErrorMsgAndThrowableWaitExceedsCustomMaxCount() throws Exception { final String expectedErrorMessage = "This is a test error message that should only repeat once!"; final Exception expectedException = new Exception("test exception"); EasyMockSupport mockSupport = new EasyMockSupport(); Logger loggerMock = mockSupport.createMock(Logger.class); AtomicInteger testAtomicInteger = new AtomicInteger(0); Capture<Exception> exceptionCaptureOne = EasyMock.newCapture(); Capture<Exception> exceptionCaptureTwo = EasyMock.newCapture(); loggerMock.error(eq(expectedErrorMessage), capture(exceptionCaptureOne)); loggerMock.error(eq(expectedErrorMessage), capture(exceptionCaptureTwo)); mockSupport.replayAll(); for (int i = 0; i < 10; i++) { Utils.logErrorMessageWithThrowableWithCounter(loggerMock, testAtomicInteger, expectedErrorMessage, expectedException, 5); } assertSame("Exception passed to Logger should have been the same instance passed into the Utils method", expectedException, exceptionCaptureOne.getValue()); assertSame("Exception passed to Logger should have been the same instance passed into the Utils method", expectedException, exceptionCaptureTwo.getValue()); mockSupport.verifyAll(); }