I am trying to figure out how to test a web/rest services written in Java Spark
and there is not many tutorials on how to do that. It is tricky to find answers due to confusion between Apache Spark
and Java Spark
.
I came across this resource but, I couldn't get it to work the way I had expected. There is also this resource and examples in Java Spark
github but they all probably use embedded server.
Anyway, Assuming that I have the following service
public class RestService implements SparkApplication {
@Override
public void init() {
get("/", (req, res) -> "Hello World!");
}
}
I would like to test the above to make sure it Hello World!
for HTTP GET
requests or not. I have the following test:
public class RestServiceTest {
//not sure if this is a good practice????
final static RestService restService = new RestService();
@BeforeClass
public static void beforeClass() {
//I have seen tests that invoked className.main(null)
//but, I don't know if its good idea to do it here?
restService.init();
Spark.awaitInitialization();
}
@AfterClass
public static void afterClass() {
Spark.stop();
}
@Test
public void testRootRoute() throws IOException {
TestResponse res = makeRequest("GET", "/");
assertEquals(200, res.status);
assertNotNull(res.body);
assertEquals("Hello World!", res.body);
}
private TestResponse makeRequest(String method, String path) throws IOException {
URL url = new URL("http://localhost:4567" + path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.connect();
String body = IOUtils.toString(connection.getInputStream());
return new TestResponse(connection.getResponseCode(), body);
}
private static class TestResponse {
public final String body;
public final int status;
public TestResponse(int status, String body) {
this.status = status;
this.body = body;
}
}
}
Running the above, the test executes successfully aand the output is shown below but, my main concern is whether this is the right method of testing a Java Spark
webapp aimed to run not in embedded server (when SparkApplication is implemented and init()
is overrided)?
T E S T S
-------------------------------------------------------
Running com.company.test.RestServiceTest
[Thread-0] INFO org.eclipse.jetty.util.log - Logging initialized @238ms
[Thread-0] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - == Spark has ignited ...
[Thread-0] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - >> Listening on 0.0.0.0:4567
[Thread-0] INFO org.eclipse.jetty.server.Server - jetty-9.3.6.v20151106
[Thread-0] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@4be514e0{HTTP/1.1,[http/1.1]}{0.0.0.0:4567}
[Thread-0] INFO org.eclipse.jetty.server.Server - Started @330ms
[main] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - >>> Spark shutting down ...
[main] INFO org.eclipse.jetty.server.ServerConnector - Stopped ServerConnector@4be514e0{HTTP/1.1,[http/1.1]}{0.0.0.0:4567}
[main] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - done
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.291 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0