TopicManagerImpl implements TopicManager { void sendSubscriptionEvent(String topic, int nb) { Collection<Session> sessions = getSessionsForTopic(topic); if (!sessions.isEmpty()) { MessageToClient messageToClient = new MessageToClient(); messageToClient.setId(topic); messageToClient.setType(MessageType.MESSAGE); messageToClient.setResponse(nb); for (Session session : sessions) { if (session.isOpen()) { session.getAsyncRemote().sendObject(messageToClient); } } } }  @Override Map<String, Collection<Session>> getSessionsByTopic(); @Override int registerTopicSession(String topic, Session session); @Override int unregisterTopicSession(String topic, Session session); @Override Collection<String> removeSessionsToTopic(Collection<Session> sessions); @Override Collection<String> removeSessionToTopics(Session session); @Override Collection<Session> getSessionsForTopic(String topic); @Override int getNumberSubscribers(String topic);  }
@Test public void testSendSubscriptionEvent() { System.out.println("sendSubscriptionEvent"); Collection<Session> sessions = new ArrayList<>(); Session session = mock(Session.class); RemoteEndpoint.Async async = mock(RemoteEndpoint.Async.class); sessions.add(session); when(session.isOpen()).thenReturn(Boolean.FALSE).thenReturn(Boolean.TRUE); when(session.getAsyncRemote()).thenReturn(async); doReturn(Collections.EMPTY_LIST).doReturn(sessions).when(instance).getSessionsForTopic(TOPIC1); instance.sendSubscriptionEvent(TOPIC1, 1); instance.sendSubscriptionEvent(TOPIC1, 2); instance.sendSubscriptionEvent(TOPIC1, 3); ArgumentCaptor<MessageToClient> captureMsg = ArgumentCaptor.forClass(MessageToClient.class); verify(async).sendObject(captureMsg.capture()); MessageToClient msg = captureMsg.getValue(); assertThat(msg.getType()).isEqualTo(MessageType.MESSAGE); assertThat(msg.getId()).isEqualTo(TOPIC1); assertThat(msg.getResponse()).isEqualTo(3); }