### Question:
MessageReceiver implements AutoCloseable { void enableRealTimeReceiving() { LoggingService.logDebug(MODULE_NAME, "Start enable real time receiving"); listener = new IOMessageListener(new MessageCallback(name)); try { consumer.setMessageListener(listener); } catch (Exception e) { listener = null; LoggingService.logError(MODULE_NAME, "Error in enabling real time listener", new AgentSystemException(e.getMessage(), e)); } LoggingService.logDebug(MODULE_NAME, "Finished enable real time receiving"); } MessageReceiver(String name, MessageConsumer consumer); void close();  }### Answer:
@Test public void testEnableRealTimeReceivingWhenConsumerIsNull() { try { messageReceiver = spy(new MessageReceiver(name, null)); messageReceiver.enableRealTimeReceiving(); Mockito.verify(messageConsumer, Mockito.never()).setMessageListener(any(IOMessageListener.class)); verifyStatic(LoggingService.class); LoggingService.logDebug(MODULE_NAME, "Start enable real time receiving"); verifyStatic(LoggingService.class); LoggingService.logError(anyString(), anyString(), any()); } catch (Exception e) { fail("This should not happen"); } }
@Test public void testEnableRealTimeReceivingWhenConsumerIsNotNull() { try { messageReceiver.enableRealTimeReceiving(); Mockito.verify(messageConsumer).setMessageListener(any(IOMessageListener.class)); } catch (Exception e) { fail("This should not happen"); } }
@Test public void throwsExceptionWhenSetHandlerIsCalledWhileEnableRealTimeReceiving() { try { PowerMockito.doThrow(mock(JMSException.class)).when(messageConsumer).setMessageListener(any()); messageReceiver.enableRealTimeReceiving(); Mockito.verify(messageConsumer).setMessageListener(any(IOMessageListener.class)); verifyStatic(LoggingService.class); LoggingService.logError(eq(MODULE_NAME), eq("Error in enabling real time listener"), any()); } catch (Exception e) { fail("This should not happen"); } }