Source:
public void start_server_and_test_requests() throws Exception { ServerSocket serverSocket = new ServerSocket(0); int localPort = serverSocket.getLocalPort(); serverSocket.close(); String uri = "http://localhost:" + localPort + "/"; Viewer.startWebServer(localPort, "class A {void fun() {}}"); try(CloseableHttpClient client = HttpClients.createMinimal()) { CloseableHttpResponse resp = client.execute(new HttpGet(uri)); assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(EntityUtils.toString(resp.getEntity())).isEqualTo(IOUtils.toString(new FileInputStream(new File("src/test/resources/viewer_result1.html")))); HttpPost httpPost = new HttpPost(uri); resp = client.execute(httpPost); assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(EntityUtils.toString(resp.getEntity())).isEqualTo(IOUtils.toString(new FileInputStream(new File("src/test/resources/viewer_result1.html")))); httpPost = new HttpPost(uri); List<NameValuePair> postParameters = new ArrayList<>(); postParameters.add(new BasicNameValuePair("javaCode", "class B{void meth() {}}")); httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8")); resp = client.execute(httpPost); assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(EntityUtils.toString(resp.getEntity())).isEqualTo(IOUtils.toString(new FileInputStream(new File("src/test/resources/viewer_result2.html")))); httpPost = new HttpPost(uri); postParameters = new ArrayList<>(); postParameters.add(new BasicNameValuePair("javaCode", "class B{}")); httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8")); resp = client.execute(httpPost); assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(EntityUtils.toString(resp.getEntity())).isEqualToIgnoringWhitespace(IOUtils.toString(new FileInputStream(new File("src/test/resources/viewer_result3.html")))); } }

Target:
public void start_server_and_test_requests() throws Exception { ServerSocket serverSocket = new ServerSocket(0); int localPort = serverSocket.getLocalPort(); serverSocket.close(); String uri = "http://localhost:" + localPort + "/"; Viewer.startWebServer(localPort, "class A {void fun() {}}"); try(CloseableHttpClient client = HttpClients.createMinimal()) { CloseableHttpResponse resp = client.execute(new HttpGet(uri)); assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(EntityUtils.toString(resp.getEntity())).isEqualTo(IOUtils.toString(new FileInputStream(new File("src/test/resources/viewer_result1.html")))); HttpPost httpPost = new HttpPost(uri); resp = client.execute(httpPost); assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(EntityUtils.toString(resp.getEntity())).isEqualTo(IOUtils.toString(new FileInputStream(new File("src/test/resources/viewer_result1.html")))); httpPost = new HttpPost(uri); List<NameValuePair> postParameters = new ArrayList<>(); postParameters.add(new BasicNameValuePair("javaCode", "class B{void meth() {}}")); httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8")); resp = client.execute(httpPost); assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(EntityUtils.toString(resp.getEntity())).isEqualTo(IOUtils.toString(new FileInputStream(new File("src/test/resources/viewer_result2.html")))); httpPost = new HttpPost(uri); postParameters = new ArrayList<>(); postParameters.add(new BasicNameValuePair("javaCode", "class B{}")); httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8")); resp = client.execute(httpPost); assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(EntityUtils.toString(resp.getEntity())).contains("<p>java.lang.NullPointerException: Unable to find a method in first class.<br/>"); } }