From 803145826525fd88d3138d12fc9513f5cb16b8a5 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sun, 23 Oct 2016 11:53:43 +0200 Subject: [PATCH] Refactor Socket tests --- .../com/baeldung/socket/EchoMultiTest.java | 20 ++++++++++--------- .../java/com/baeldung/socket/EchoTest.java | 16 +++++++++------ .../com/baeldung/socket/GreetServerTest.java | 13 ++++++++---- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/socket/EchoMultiTest.java b/core-java/src/test/java/com/baeldung/socket/EchoMultiTest.java index ef08385551..ccb5f48ddb 100644 --- a/core-java/src/test/java/com/baeldung/socket/EchoMultiTest.java +++ b/core-java/src/test/java/com/baeldung/socket/EchoMultiTest.java @@ -1,7 +1,6 @@ package com.baeldung.socket; -import org.junit.After; -import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import java.util.concurrent.Executors; @@ -10,18 +9,22 @@ import static org.junit.Assert.assertEquals; public class EchoMultiTest { - { - Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(5555)); - } + private static final Integer PORT = 5555; + @BeforeClass + public static void start() throws InterruptedException { + Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(PORT)); + Thread.sleep(500); + } @Test public void givenClient1_whenServerResponds_thenCorrect() { EchoClient client = new EchoClient(); - client.startConnection("127.0.0.1", 5555); + client.startConnection("127.0.0.1", PORT); String msg1 = client.sendMessage("hello"); String msg2 = client.sendMessage("world"); String terminate = client.sendMessage(""); + assertEquals(msg1, "hello"); assertEquals(msg2, "world"); assertEquals(terminate, "bye"); @@ -31,7 +34,7 @@ public class EchoMultiTest { @Test public void givenClient2_whenServerResponds_thenCorrect() { EchoClient client = new EchoClient(); - client.startConnection("127.0.0.1", 5555); + client.startConnection("127.0.0.1", PORT); String msg1 = client.sendMessage("hello"); String msg2 = client.sendMessage("world"); String terminate = client.sendMessage(""); @@ -44,7 +47,7 @@ public class EchoMultiTest { @Test public void givenClient3_whenServerResponds_thenCorrect() { EchoClient client = new EchoClient(); - client.startConnection("127.0.0.1", 5555); + client.startConnection("127.0.0.1", PORT); String msg1 = client.sendMessage("hello"); String msg2 = client.sendMessage("world"); String terminate = client.sendMessage(""); @@ -53,5 +56,4 @@ public class EchoMultiTest { assertEquals(terminate, "bye"); client.stopConnection(); } - } diff --git a/core-java/src/test/java/com/baeldung/socket/EchoTest.java b/core-java/src/test/java/com/baeldung/socket/EchoTest.java index c12bd28a29..76391f0f20 100644 --- a/core-java/src/test/java/com/baeldung/socket/EchoTest.java +++ b/core-java/src/test/java/com/baeldung/socket/EchoTest.java @@ -2,6 +2,7 @@ package com.baeldung.socket; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import java.util.concurrent.Executors; @@ -9,15 +10,19 @@ import java.util.concurrent.Executors; import static org.junit.Assert.assertEquals; public class EchoTest { - { - Executors.newSingleThreadExecutor().submit(() -> new EchoServer().start(4444)); - } + private static final Integer PORT = 4444; - EchoClient client = new EchoClient(); + @BeforeClass + public static void start() throws InterruptedException { + Executors.newSingleThreadExecutor().submit(() -> new EchoServer().start(PORT)); + Thread.sleep(500); + } + + private EchoClient client = new EchoClient(); @Before public void init() { - client.startConnection("127.0.0.1", 4444); + client.startConnection("127.0.0.1", PORT); } @Test @@ -37,5 +42,4 @@ public class EchoTest { public void tearDown() { client.stopConnection(); } - } diff --git a/core-java/src/test/java/com/baeldung/socket/GreetServerTest.java b/core-java/src/test/java/com/baeldung/socket/GreetServerTest.java index fedc32fb39..caf56a8c76 100644 --- a/core-java/src/test/java/com/baeldung/socket/GreetServerTest.java +++ b/core-java/src/test/java/com/baeldung/socket/GreetServerTest.java @@ -2,6 +2,7 @@ package com.baeldung.socket; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import java.util.concurrent.Executors; @@ -10,16 +11,20 @@ import static org.junit.Assert.assertEquals; public class GreetServerTest { - GreetClient client; + private GreetClient client; - { - Executors.newSingleThreadExecutor().submit(() -> new GreetServer().start(6666)); + private static final Integer PORT = 6666; + + @BeforeClass + public static void start() throws InterruptedException { + Executors.newSingleThreadExecutor().submit(() -> new GreetServer().start(PORT)); + Thread.sleep(500); } @Before public void init() { client = new GreetClient(); - client.startConnection("127.0.0.1", 6666); + client.startConnection("127.0.0.1", PORT); }