Refactor Socket tests

This commit is contained in:
Grzegorz Piwowarek 2016-10-23 11:53:43 +02:00
parent 4670b8dbda
commit 8031458265
3 changed files with 30 additions and 19 deletions

View File

@ -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();
}
}

View File

@ -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;
@BeforeClass
public static void start() throws InterruptedException {
Executors.newSingleThreadExecutor().submit(() -> new EchoServer().start(PORT));
Thread.sleep(500);
}
EchoClient client = new EchoClient();
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();
}
}

View File

@ -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);
}