### Question:
NotificationService { public void subscribedEntityChanged(SubscriptionAwareEntity entity) { Collection<JCUser> subscribers = subscriptionService.getAllowedSubscribers(entity); filterSubscribers(subscribers, entity); for (JCUser user : subscribers) { mailService.sendUpdatesOnSubscription(user, entity); } } NotificationService(
            UserService userService,
            MailService mailService,
            SubscriptionService subscriptionService,
            PluginLoader pluginLoader); void subscribedEntityChanged(SubscriptionAwareEntity entity); void sendNotificationAboutTopicMoved(Topic topic); void sendNotificationAboutRemovingTopic(Topic topic); void sendNotificationAboutTopicCreated(Topic topic);  }### Answer:
@Test public void testTopicChanged() throws MailingFailedException { topic.getSubscribers().add(user1); topic.getSubscribers().add(user2); topic.getSubscribers().add(currentUser); when(subscriptionService.getAllowedSubscribers(topic)).thenReturn(topic.getSubscribers()); service.subscribedEntityChanged(topic); verify(mailService, times(2)).sendUpdatesOnSubscription(any(JCUser.class), eq(topic)); verify(mailService).sendUpdatesOnSubscription(user1, topic); verify(mailService).sendUpdatesOnSubscription(user2, topic); assertEquals(topic.getSubscribers().size(), 2); }
@Test public void testBranchChanged() throws MailingFailedException { branch.getSubscribers().add(user1); branch.getSubscribers().add(user2); branch.getSubscribers().add(currentUser); when(subscriptionService.getAllowedSubscribers(branch)).thenReturn(branch.getSubscribers()); service.subscribedEntityChanged(branch); verify(mailService, times(2)).sendUpdatesOnSubscription( any(JCUser.class), eq(branch)); verify(mailService).sendUpdatesOnSubscription(user1, branch); verify(mailService).sendUpdatesOnSubscription(user2, branch); assertEquals(branch.getSubscribers().size(), 2); }
@Test public void testTopicChangedSelfSubscribed() throws MailingFailedException { when(userService.getCurrentUser()).thenReturn(user1); topic.getSubscribers().add(user1); topic.getSubscribers().add(user2); when(subscriptionService.getAllowedSubscribers(topic)).thenReturn(topic.getSubscribers()); service.subscribedEntityChanged(topic); verify(mailService).sendUpdatesOnSubscription(user2, topic); verifyNoMoreInteractions(mailService); }
@Test public void testBranchChangedSelfSubscribed() throws MailingFailedException { when(userService.getCurrentUser()).thenReturn(user1); branch.getSubscribers().add(user1); branch.getSubscribers().add(user2); when(subscriptionService.getAllowedSubscribers(branch)).thenReturn(branch.getSubscribers()); service.subscribedEntityChanged(branch); verify(mailService).sendUpdatesOnSubscription(user2, branch); verifyNoMoreInteractions(mailService); }
@Test public void testTopicChangedNoSubscribers() { service.subscribedEntityChanged(topic); verifyZeroInteractions(mailService); }
@Test public void testBranchChangedNoSubscribers() { service.subscribedEntityChanged(branch); verifyZeroInteractions(mailService); }