Mit folgendem Testcode könnt ihr einen Server lokal simulieren:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.List; import junit.framework.TestCase; public class TestWithMockServer extends TestCase { ServerSocket sock = new ServerSocket(42523); public TestWithMockServer() throws IOException { } public Thread letClientConnect(final String[] communication) throws IOException { Thread t; (t = new Thread() { public void run() { try { BufferedReader in; PrintStream out; Socket clientSocket; clientSocket = sock.accept(); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); out = new PrintStream(clientSocket.getOutputStream(), true); for (int i = 0; i < communication.length; i++) { switch (communication[i].charAt(0)) { case '+': String outS = communication[i].substring(1); System.out.println("OUT: " + outS); out.println(outS); break; case '-': String read = in.readLine(); String excpected = communication[i].substring(1); System.out.println("IN : " + read + " , " + excpected); assertEquals(excpected, read); break; default: out.println(communication[i]); } } out.close(); in.close(); clientSocket.close(); sock.close(); } catch (Exception e) { throw new RuntimeException(e); } } }).start(); return t; } public void testListGames() throws Exception { Thread t = letClientConnect(new String[] { "-PROTOCOL GASP 1.0", "+OK", "-USER Chris", "+OK", "-PASS SecretPass", "+OK", "-LIST PLAYERS", "+PLAYER Chris", "+OK", "-LIST GAMES", "+GAME Abalone", "+OK", "-LOGOUT", "+BYE" }); Client c = new Client(); c.connect("127.0.0.1", 42523, "Chris", "SecretPass"); Lists = c.list(Client.ListType.PLAYERS); assertNotNull(s); assertEquals(1, s.size()); assertTrue(s.contains("Chris")); List s2 = c.list(Client.ListType.GAMES); assertEquals(1, s2.size()); assertTrue(s2.contains("Abalone")); assertEquals(ResponseType.BYE, c.logOut().getResponseType()); t.join(); }