Package com.oorian.testing
Class MockWebsocketConnection
java.lang.Object
com.oorian.testing.MockWebsocketConnection
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionConstructs a new MockWebsocketConnection in the open state. -
Method Summary
Modifier and TypeMethodDescriptionvoidClears all captured messages, resetting the message history.voidclose()Marks this connection as closed.Returns the most recently sent message, ornullif no messages have been sent.intReturns the number of messages sent through this connection.Returns an unmodifiable list of all messages sent through this connection.booleanisClosed()Returns whether this connection has been closed.voidsendMessage(String text) Stores the text message for later retrieval viagetMessages().
-
Constructor Details
-
MockWebsocketConnection
public MockWebsocketConnection()Constructs a new MockWebsocketConnection in the open state.
-
-
Method Details
-
sendMessage
Stores the text message for later retrieval viagetMessages().- Specified by:
sendMessagein interfaceWebsocketConnection- 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:
closein interfaceWebsocketConnection
-
getMessages
Returns an unmodifiable list of all messages sent through this connection.- Returns:
- the list of sent messages in order
-
getLastMessage
Returns the most recently sent message, ornullif 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.
-