### Question:
MessageBusUtil { public void publishMessage(Message message) { LoggingService.logDebug(MODULE_NAME, "Start publish message"); long timestamp = System.currentTimeMillis(); StatusReporter.setMessageBusStatus().increasePublishedMessagesPerMicroservice(message.getPublisher()); message.setId(messageBus.getNextId()); message.setTimestamp(timestamp); MessagePublisher publisher = messageBus.getPublisher(message.getPublisher()); if (publisher != null) { try { publisher.publish(message); } catch (Exception e) { LoggingService.logError(MODULE_NAME, "Unable to send message : Message Publisher (" + publisher.getName()+ ")", new AgentSystemException(e.getMessage(), e)); } } LoggingService.logDebug(MODULE_NAME, "Finishing publish message"); } 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 testPublishMessageWhenPublisherIsNull() { try { PowerMockito.when(messageBus.getPublisher(message.getPublisher())).thenReturn(null); messageBusUtil.publishMessage(message); Mockito.verify(messageBus).getPublisher(any()); Mockito.verify(messagePublisher, Mockito.never()).publish(any(Message.class)); PowerMockito.verifyStatic(LoggingService.class); LoggingService.logDebug(MODULE_NAME, "Start publish message"); PowerMockito.verifyStatic(LoggingService.class); LoggingService.logDebug(MODULE_NAME, "Finishing publish message"); } catch (Exception e) { fail("This should not happen"); } }
@Test public void testPublishMessageWhenPublisherIsNotNull() { try { PowerMockito.when(messageBus.getPublisher(message.getPublisher())).thenReturn(messagePublisher); messageBusUtil.publishMessage(message); Mockito.verify(messageBus).getPublisher(any()); Mockito.verify(messagePublisher).publish(any(Message.class)); } catch (Exception e) { fail("This should not happen"); } }
@Test public void throwsExceptionWhenPublisherIsCalled() { PowerMockito.when(messageBus.getPublisher(message.getPublisher())).thenReturn(messagePublisher); PowerMockito.when(messagePublisher.getName()).thenReturn("MP"); try { PowerMockito.doThrow(mock(Exception.class)).when(messagePublisher).publish(any()); messageBusUtil.publishMessage(message); Mockito.verify(messageBus).getPublisher(any()); Mockito.verify(messagePublisher).publish(any(Message.class)); PowerMockito.verifyStatic(LoggingService.class); LoggingService.logError(eq(MODULE_NAME), eq("Unable to send message : Message Publisher (MP)"), any()); } catch (Exception e) { fail("This Should not happen"); } }