AbstractMailPoller { public void poll() { try { MessageMonitoringUtil.updateAgentSettingsCacheTimeoutValue(); mailReceiver.handleMessages(messageHandler); } catch (MailClientException e) { handleException(e); } } AbstractMailPoller(MailReceiver mailReceiver, MessageHandler messageHandler); void poll(); abstract void handleException(MailClientException e);  }
@Test public void canMailPollerInvokeHandler() throws MailClientException { MailReceiver mockMailReceiver = mock(MailReceiver.class); MessageHandler mockMessageHandler = mock(MessageHandler.class); MailClientException mockException = mock(MailClientException.class); when(mockException.getMessage()).thenReturn(EXCEPTION_MSG); AbstractMailPoller testMailPoller = new AbstractMailPoller(mockMailReceiver, mockMessageHandler) { @Override public void handleException(MailClientException e) { assertEquals(EXCEPTION_MSG, e.getMessage()); } }; testMailPoller.poll(); verify(mockMailReceiver).handleMessages(mockMessageHandler); verify(mockException, times(0)).getMessage(); }
@Test public void canMailPollerHandleException() throws MailClientException { MailReceiver mockMailReceiver = mock(MailReceiver.class); MessageHandler mockMessageHandler = mock(MessageHandler.class); MailClientException mockException = mock(MailClientException.class); when(mockException.getMessage()).thenReturn(EXCEPTION_MSG); when(mockMailReceiver.handleMessages(mockMessageHandler)).thenThrow(mockException); AbstractMailPoller testMailPoller = new AbstractMailPoller(mockMailReceiver, mockMessageHandler) { @Override public void handleException(MailClientException e) { assertEquals(EXCEPTION_MSG, e.getMessage()); } }; testMailPoller.poll(); verify(mockMailReceiver).handleMessages(mockMessageHandler); verify(mockException).getMessage(); }
@Test public void testPollMethodCanInvokeHandler() throws MailClientException { MailClientException mockException = mock(MailClientException.class); when(mockException.getMessage()).thenReturn(EXCEPTION_MSG); AbstractMailPoller poller = new AbstractMailPoller(mailReceiver, messageHandler) { @Override public void handleException(MailClientException e) { assertEquals(EXCEPTION_MSG, e.getMessage()); } }; poller.poll(); verify(mailReceiver).handleMessages(messageHandler); verify(mockException, times(0)).getMessage(); }
@Test public void testPollMethodCanHandleException() throws MailClientException { MailClientException mockException = mock(MailClientException.class); when(mockException.getMessage()).thenReturn(EXCEPTION_MSG); when(mailReceiver.handleMessages(messageHandler)).thenThrow(mockException); AbstractMailPoller poller = new AbstractMailPoller(mailReceiver, messageHandler) { @Override public void handleException(MailClientException e) { assertEquals(EXCEPTION_MSG, e.getMessage()); } }; poller.poll(); verify(mailReceiver).handleMessages(messageHandler); verify(mockException).getMessage(); }