### Question:
WebSocketServiceImpl implements WebSocketService { @Override public void addMessageHandler(Consumer<String> consumer) throws WebSocketServiceException { if (session == null) { throw new WebSocketServiceException( "You first must connect to ws server in order to receive messages."); } if (!session.getMessageHandlers().isEmpty()) { throw new WebSocketServiceException("You are already subscribed to this web socket service."); } session.addMessageHandler( new MessageHandler.Whole<String>() { @Override public void onMessage(String message) { LOGGER.debug("Received message {} on {}", message, session.getRequestURI()); consumer.accept(message); } }); } WebSocketServiceImpl(Session session); private  WebSocketServiceImpl(); static WebSocketService create(URI uri); @Override void connect(URI uri); @Override void send(String message); @Override void addMessageHandler(Consumer<String> consumer); @Override void close(); @Override boolean closed(); @SuppressWarnings("unchecked") static WebSocketContainer getWebSocketContainer(); static final String WEB_SOCKET_CONTAINER_FACTORY_PROPERTY; }### Answer:
@Test(expected = WebSocketServiceException.class) public void testAddMessageHandlerThrowsExceptionIfNotConnected() throws WebSocketServiceException { WebSocketServiceImpl socketService = new WebSocketServiceImpl(null); socketService.addMessageHandler(message -> {}); }
@Test(expected = WebSocketServiceException.class) public void testAddMessageHandlerThrowsExceptionIfHandlerAlreadyAdded() throws WebSocketServiceException { Session session = mock(Session.class); Set<MessageHandler> messageHandlerSet = new HashSet<>(); messageHandlerSet.add(null); expect(session.getMessageHandlers()).andReturn(messageHandlerSet); replay(session); WebSocketServiceImpl socketService = new WebSocketServiceImpl(session); socketService.addMessageHandler(message -> {}); verify(session); }