### Question:
MessageBusUtil { public List<Message> getMessages(String receiver) { LoggingService.logDebug(MODULE_NAME, "Starting get message"); List<Message> messages = new ArrayList<>(); MessageReceiver rec = messageBus.getReceiver(receiver); if (rec != null) { try { messages = rec.getMessages(); } catch (Exception e) { LoggingService.logError(MODULE_NAME, "unable to receive messages : Message Receiver (" + receiver + ")", new AgentSystemException(e.getMessage(), e)); } } LoggingService.logDebug(MODULE_NAME, "Finishing get message"); return messages; } MessageBusUtil(); void publishMessage(Message message); List<Message> getMessages(String receiver); List<Message> messageQuery(String publisher, String receiver, long from, long to);  }### Answer:
@Test public void testGetMessagesWhenMessageReceiverIsNull() { try { PowerMockito.when(messageBus.getReceiver(any())).thenReturn(null); assertEquals(0, messageBusUtil.getMessages("receiver").size()); Mockito.verify(messageBus).getReceiver(any()); Mockito.verify(messageReceiver, Mockito.never()).getMessages(); PowerMockito.verifyStatic(LoggingService.class); LoggingService.logDebug(MODULE_NAME, "Starting get message"); PowerMockito.verifyStatic(LoggingService.class); LoggingService.logDebug(MODULE_NAME, "Finishing get message"); } catch (Exception e) { fail("This should not happen"); } }
@Test public void testGetMessagesWhenMessageReceiverIsNotNull() { try { assertEquals(messages, messageBusUtil.getMessages("receiver")); Mockito.verify(messageBus).getReceiver(any()); Mockito.verify(messageReceiver).getMessages(); } catch (Exception e) { fail("This should not happen"); } }
@Test public void throwsExceptionWhenMessagePublisherIsCalledInGetMessages() { try { PowerMockito.doThrow(mock(Exception.class)).when(messageReceiver).getMessages(); assertEquals(0, messageBusUtil.getMessages("receiver").size()); Mockito.verify(messageBus).getReceiver(any()); Mockito.verify(messageReceiver).getMessages(); PowerMockito.verifyStatic(LoggingService.class); LoggingService.logError(eq(MODULE_NAME), eq("unable to receive messages : Message Receiver (receiver)"), any()); } catch (Exception e) { fail("This should not happen"); } }