Skip to content

Commit

Permalink
make sure server port in test context is only defined once.
Browse files Browse the repository at this point in the history
  • Loading branch information
ideadapt committed Mar 11, 2022
1 parent 339c54e commit 28c62d9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
public class FunctionalTest {

public static class TodoApplication implements SparkApplication {
private HttpServer httpServer;
private final HttpServer httpServer;

public TodoApplication(Integer port) {
httpServer = new HttpServer(String.valueOf(port), true);
}

@Override
public void init() {
httpServer = new HttpServer("4567", true);
httpServer.start();
}

Expand All @@ -26,7 +29,7 @@ public void destroy() {
}

@Rule
public SparkServer<TodoApplication> httpClient = new SparkServer<>(TodoApplication.class);
public SparkServer<TodoApplication> httpClient = new SparkServer<>(TodoApplication.class, 4567);

public HttpResponse<String> executeGet(String path, String acceptType) {
HttpRequest.Builder method = httpClient.get(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.junit.rules.ExternalResource;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import spark.Service;
import spark.servlet.SparkApplication;

import java.io.IOException;
Expand All @@ -17,17 +16,15 @@ public class SparkServer<T extends SparkApplication> extends ExternalResource {
private final Class<T> sparkApplicationClass;
private T sparkApplication;
private final String protocolHostPort;
private int port;
private final HttpClient httpClient;
private final HttpRequest.Builder requestBuilder = HttpRequest.newBuilder();

SparkServer(Class<T> sparkApplicationClass) {
this(sparkApplicationClass, Service.SPARK_DEFAULT_PORT);
}

SparkServer(Class<T> sparkApplicationClass, int port) {
this.sparkApplicationClass = sparkApplicationClass;
this.protocolHostPort = "http://localhost:" + port;
this.httpClient = HttpClient.newHttpClient();
this.port = port;
}

@Override
Expand All @@ -37,7 +34,7 @@ public Statement apply(Statement base, Description description) {

@Override
protected void before() throws Throwable {
this.sparkApplication = this.sparkApplicationClass.getDeclaredConstructor().newInstance();
this.sparkApplication = this.sparkApplicationClass.getDeclaredConstructor(Integer.class).newInstance(port);
this.sparkApplication.init();
}

Expand Down

0 comments on commit 28c62d9

Please sign in to comment.