Refactor Socket tests
This commit is contained in:
parent
4670b8dbda
commit
8031458265
@ -1,7 +1,6 @@
|
|||||||
package com.baeldung.socket;
|
package com.baeldung.socket;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@ -10,18 +9,22 @@ import static org.junit.Assert.assertEquals;
|
|||||||
|
|
||||||
public class EchoMultiTest {
|
public class EchoMultiTest {
|
||||||
|
|
||||||
{
|
private static final Integer PORT = 5555;
|
||||||
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(5555));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void start() throws InterruptedException {
|
||||||
|
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(PORT));
|
||||||
|
Thread.sleep(500);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenClient1_whenServerResponds_thenCorrect() {
|
public void givenClient1_whenServerResponds_thenCorrect() {
|
||||||
EchoClient client = new EchoClient();
|
EchoClient client = new EchoClient();
|
||||||
client.startConnection("127.0.0.1", 5555);
|
client.startConnection("127.0.0.1", PORT);
|
||||||
String msg1 = client.sendMessage("hello");
|
String msg1 = client.sendMessage("hello");
|
||||||
String msg2 = client.sendMessage("world");
|
String msg2 = client.sendMessage("world");
|
||||||
String terminate = client.sendMessage("");
|
String terminate = client.sendMessage("");
|
||||||
|
|
||||||
assertEquals(msg1, "hello");
|
assertEquals(msg1, "hello");
|
||||||
assertEquals(msg2, "world");
|
assertEquals(msg2, "world");
|
||||||
assertEquals(terminate, "bye");
|
assertEquals(terminate, "bye");
|
||||||
@ -31,7 +34,7 @@ public class EchoMultiTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenClient2_whenServerResponds_thenCorrect() {
|
public void givenClient2_whenServerResponds_thenCorrect() {
|
||||||
EchoClient client = new EchoClient();
|
EchoClient client = new EchoClient();
|
||||||
client.startConnection("127.0.0.1", 5555);
|
client.startConnection("127.0.0.1", PORT);
|
||||||
String msg1 = client.sendMessage("hello");
|
String msg1 = client.sendMessage("hello");
|
||||||
String msg2 = client.sendMessage("world");
|
String msg2 = client.sendMessage("world");
|
||||||
String terminate = client.sendMessage("");
|
String terminate = client.sendMessage("");
|
||||||
@ -44,7 +47,7 @@ public class EchoMultiTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenClient3_whenServerResponds_thenCorrect() {
|
public void givenClient3_whenServerResponds_thenCorrect() {
|
||||||
EchoClient client = new EchoClient();
|
EchoClient client = new EchoClient();
|
||||||
client.startConnection("127.0.0.1", 5555);
|
client.startConnection("127.0.0.1", PORT);
|
||||||
String msg1 = client.sendMessage("hello");
|
String msg1 = client.sendMessage("hello");
|
||||||
String msg2 = client.sendMessage("world");
|
String msg2 = client.sendMessage("world");
|
||||||
String terminate = client.sendMessage("");
|
String terminate = client.sendMessage("");
|
||||||
@ -53,5 +56,4 @@ public class EchoMultiTest {
|
|||||||
assertEquals(terminate, "bye");
|
assertEquals(terminate, "bye");
|
||||||
client.stopConnection();
|
client.stopConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.baeldung.socket;
|
|||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@ -9,15 +10,19 @@ import java.util.concurrent.Executors;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class EchoTest {
|
public class EchoTest {
|
||||||
{
|
private static final Integer PORT = 4444;
|
||||||
Executors.newSingleThreadExecutor().submit(() -> new EchoServer().start(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
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
client.startConnection("127.0.0.1", 4444);
|
client.startConnection("127.0.0.1", PORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -37,5 +42,4 @@ public class EchoTest {
|
|||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
client.stopConnection();
|
client.stopConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.baeldung.socket;
|
|||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@ -10,16 +11,20 @@ import static org.junit.Assert.assertEquals;
|
|||||||
|
|
||||||
public class GreetServerTest {
|
public class GreetServerTest {
|
||||||
|
|
||||||
GreetClient client;
|
private GreetClient client;
|
||||||
|
|
||||||
{
|
private static final Integer PORT = 6666;
|
||||||
Executors.newSingleThreadExecutor().submit(() -> new GreetServer().start(6666));
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void start() throws InterruptedException {
|
||||||
|
Executors.newSingleThreadExecutor().submit(() -> new GreetServer().start(PORT));
|
||||||
|
Thread.sleep(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
client = new GreetClient();
|
client = new GreetClient();
|
||||||
client.startConnection("127.0.0.1", 6666);
|
client.startConnection("127.0.0.1", PORT);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user