### Question:
WebSocketMessageRouter { public void sendMessage(final String sessionId, final SendMessage sendMessage) throws IOException { if (!StringUtils.isEmpty(sessionId)) { final WebSocketSession session = getSessionOrFail(sessionId); sendMessage.send(session); } else { sessions.keySet().forEach(itrSessionId -> { try { final WebSocketSession session = getSessionOrFail(itrSessionId); sendMessage.send(session); } catch (IOException e) { logger.warn("Failed to send message to session {} due to {}", itrSessionId, e, e); } }); } } WebSocketMessageRouter(final String endpointId); synchronized void registerProcessor(final Processor processor); boolean isProcessorRegistered(final Processor processor); synchronized void deregisterProcessor(final Processor processor); void captureSession(final WebSocketSession session); void onWebSocketClose(final String sessionId, final int statusCode, final String reason); void onWebSocketText(final String sessionId, final String message); void onWebSocketBinary(final String sessionId, final byte[] payload, final int offset, final int length); void sendMessage(final String sessionId, final SendMessage sendMessage); void disconnect(final String sessionId, final String reason); boolean containsSession(final String sessionId);  }### Answer:
@Test public void testSendMessage() throws Exception { final WebSocketMessageRouter router = new WebSocketMessageRouter("endpoint-id"); final Processor processor1 = mock(Processor.class); when(processor1.getIdentifier()).thenReturn("processor-1"); final AbstractWebSocketSession session = mock(AbstractWebSocketSession.class); when(session.getSessionId()).thenReturn("session-1"); doAnswer(invocation -> { assertEquals("message", invocation.getArgumentAt(0, String.class)); return null; }).when(session).sendString(anyString()); router.registerProcessor(processor1); router.captureSession(session); router.sendMessage("session-1", sender -> sender.sendString("message")); try { router.sendMessage("session-2", sender -> sender.sendString("message")); fail("Should fail because there's no session with id session-2."); } catch (IllegalStateException e) { } }