### Question:
MessageReceiver implements AutoCloseable { synchronized List<Message> getMessages() throws Exception { LoggingService.logDebug(MODULE_NAME, String.format("Start getting message \"%s\"", name)); List<Message> result = new ArrayList<>(); if (consumer != null || listener == null) { Message message = getMessage(); while (message != null) { result.add(message); message = getMessage(); } } LoggingService.logDebug(MODULE_NAME, String.format("Finished getting message \"%s\"", name)); return result; } MessageReceiver(String name, MessageConsumer consumer); void close();  }### Answer:
@Test public void testGetMessagesWhenClientConsumerReceivesNull() { try { PowerMockito.when(messageConsumer.receiveNoWait()).thenReturn(null); assertEquals(0, messageReceiver.getMessages().size()); Mockito.verify(messageConsumer, times(1)).receiveNoWait(); Mockito.verify(textMessage, Mockito.never()).acknowledge(); Mockito.verify(messageConsumer, Mockito.never()).setMessageListener(any(IOMessageListener.class)); PowerMockito.verifyPrivate(messageReceiver, times(1)) .invoke("getMessage"); verifyStatic(LoggingService.class); LoggingService.logDebug(MODULE_NAME, String.format("Finished getting message \"%s\"", name)); } catch (Exception e) { fail("This should not happen"); } }
@Test public void testGetMessagesWhenListenerIsNull() { try { assertEquals(message, messageReceiver.getMessages().get(0)); Mockito.verify(messageConsumer, times(2)).receiveNoWait(); Mockito.verify(textMessage).acknowledge(); Mockito.verify(messageConsumer, Mockito.never()).setMessageListener(any(IOMessageListener.class)); PowerMockito.verifyPrivate(messageReceiver, times(2)) .invoke("getMessage"); } catch (Exception e) { fail("This should not happen"); } }