Class MockWebsocketConnection

java.lang.Object
com.oorian.testing.MockWebsocketConnection
All Implemented Interfaces:
WebsocketConnection

public class MockWebsocketConnection extends Object implements WebsocketConnection
Mock implementation of WebsocketConnection for testing server-side components without requiring a real browser WebSocket connection.

This mock captures all sent messages in an in-memory list for later verification, and tracks whether the connection has been closed. It enables unit testing of page lifecycle, event handling, and server-push logic.

Usage:


 MockWebsocketConnection ws = new MockWebsocketConnection();
 page.handleWebsocketOpen(ws);

 // Verify messages sent to client
 List<String> messages = ws.getMessages();
 assert messages.size() == 1;
 assert messages.get(0).contains("update");

 // Check connection state
 assert !ws.isClosed();
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • MockWebsocketConnection

      public MockWebsocketConnection()
      Constructs a new MockWebsocketConnection in the open state.
  • Method Details

    • sendMessage

      public void sendMessage(String text)
      Stores the text message for later retrieval via getMessages().
      Specified by:
      sendMessage in interface WebsocketConnection
      Parameters:
      text - The text message to send.
      Throws:
      IllegalStateException - if the connection has been closed.
    • close

      public void close()
      Marks this connection as closed.
      Specified by:
      close in interface WebsocketConnection
    • getMessages

      public List<String> getMessages()
      Returns an unmodifiable list of all messages sent through this connection.
      Returns:
      the list of sent messages in order
    • getLastMessage

      public String getLastMessage()
      Returns the most recently sent message, or null if no messages have been sent.
      Returns:
      the last message sent, or null
    • getMessageCount

      public int getMessageCount()
      Returns the number of messages sent through this connection.
      Returns:
      the message count
    • isClosed

      public boolean isClosed()
      Returns whether this connection has been closed.
      Returns:
      true if close() has been called
    • clearMessages

      public void clearMessages()
      Clears all captured messages, resetting the message history.