From 6a7ab650b9ccebd60097a7abcf8513c666abd5d9 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 14:52:18 +0100 Subject: [PATCH] refactoring: TestClientHandlerConstructor --- .../java/TestClientHandlerConstructor.java | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/test/java/TestClientHandlerConstructor.java b/src/test/java/TestClientHandlerConstructor.java index 0a20691..eb1c0ff 100644 --- a/src/test/java/TestClientHandlerConstructor.java +++ b/src/test/java/TestClientHandlerConstructor.java @@ -3,29 +3,28 @@ import org.junit.Test; import java.io.*; import java.net.*; -public class TestClientHandlerConstructor { +public class ClientHandlerTest { @Test - public void testConstructor() { - Socket mockSocket = new Socket(); + // Create a mock Socket + Socket mockSocket = new Socket(); + // Create a ChatServer instance + int port = 12345; // Use any available port for testing + ChatServer mockChatServer = new ChatServer(port); - int port = 12345; - - - ChatServer mockChatServer = new ChatServer(port); + // Create a ClientHandler instance using the constructor + ClientHandler clientHandler = new ClientHandler(mockChatServer, mockSocket); - - ClientHandler clientHandler = new ClientHandler(mockChatServer, mockSocket); + // Wait for a short time to allow the ClientHandler to be added to the ChatServer's list of clients + try { + Thread.sleep(1000); // Adjust the sleep duration as needed + } catch (InterruptedException e) { + e.printStackTrace(); + } - try { - Thread.sleep(1000); // Adjust the sleep duration as needed - } catch (InterruptedException e) { - e.printStackTrace(); - } - - + // Assert that the clientHandler was added to the list of clients in the ChatServer assertTrue(mockChatServer.getClients().contains(clientHandler)); } }