### Question:
MessageBusUtil { public List<Message> messageQuery(String publisher, String receiver, long from, long to) { LoggingService.logDebug(MODULE_NAME, "Starting message query"); Route route = messageBus.getRoutes().get(publisher); if (to < from || route == null || !route.getReceivers().contains(receiver)) return null; MessagePublisher messagePublisher = messageBus.getPublisher(publisher); if (messagePublisher == null) return null; LoggingService.logDebug(MODULE_NAME, "Finishing message query"); return messagePublisher.messageQuery(from, to); } 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 testMessageQueryWhenRouteIsNull() { try { PowerMockito.when(routes.get(any())).thenReturn(null); assertNull(messageBusUtil.messageQuery("publisher", "receiver", currentTimeMillis(), 100l)); Mockito.verify(messageBus).getRoutes(); Mockito.verify(messageBus, Mockito.never()).getPublisher(any()); Mockito.verify(messagePublisher, Mockito.never()).messageQuery(anyLong(), anyLong()); PowerMockito.verifyStatic(LoggingService.class); LoggingService.logDebug(MODULE_NAME, "Starting message query"); PowerMockito.verifyStatic(LoggingService.class, Mockito.never()); LoggingService.logDebug(MODULE_NAME, "Finishing message query"); } catch (Exception e) { fail("This should not happen"); } }
@Test public void testMessageQueryWhenInputParamToIsLessThanFrom() { try { assertNull(messageBusUtil.messageQuery("publisher", "receiver", currentTimeMillis(), 100l)); Mockito.verify(messageBus).getRoutes(); Mockito.verify(messageBus, Mockito.never()).getPublisher(any()); Mockito.verify(messagePublisher, Mockito.never()).messageQuery(anyLong(), anyLong()); PowerMockito.verifyStatic(LoggingService.class); LoggingService.logDebug(MODULE_NAME, "Starting message query"); PowerMockito.verifyStatic(LoggingService.class, Mockito.never()); LoggingService.logDebug(MODULE_NAME, "Finishing message query"); } catch (Exception e) { fail("This should not happen"); } }
@Test public void testMessageQueryWhenRouteDoseNotHaveReceiver() { try { PowerMockito.when(receivers.contains(eq("receiver"))).thenReturn(false); assertNull(messageBusUtil.messageQuery("publisher", "receiver", 100l, currentTimeMillis())); Mockito.verify(messageBus).getRoutes(); Mockito.verify(route).getReceivers(); Mockito.verify(messageBus, Mockito.never()).getPublisher(any()); Mockito.verify(messagePublisher, Mockito.never()).messageQuery(anyLong(), anyLong()); } catch (Exception e) { fail("This should not happen"); } }
@Test public void testMessageQueryWhenRouteHaveReceiverButNotPublisher() { try { PowerMockito.when(messageBus.getPublisher(any())).thenReturn(null); assertNull(messageBusUtil.messageQuery("publisher", "receiver", 100l, currentTimeMillis())); Mockito.verify(messageBus).getRoutes(); Mockito.verify(route).getReceivers(); Mockito.verify(messageBus).getPublisher(any()); Mockito.verify(messagePublisher, Mockito.never()).messageQuery(anyLong(), anyLong()); } catch (Exception e) { fail("This should not happen"); } }
@Test public void testMessageQueryWhenRouteHaveReceiverAndPublisher() { try { PowerMockito.when(messagePublisher.messageQuery(anyLong(), anyLong())).thenReturn(messages); assertEquals(messages, messageBusUtil.messageQuery("publisher", "receiver", 100l, currentTimeMillis())); Mockito.verify(messageBus).getRoutes(); Mockito.verify(messageBus).getPublisher(any()); Mockito.verify(messagePublisher).messageQuery(anyLong(), anyLong()); } catch (Exception e) { fail("This should not happen"); } }