Testing Java Spark Microservices app that implements SparkApplication interface
Asked Answered
I

1

2

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
Irreplaceable answered 9/6, 2016 at 2:26 Comment(2)
I don't know if it's the right method of testing. Consider posting your question to the Java Spark google group (I hope you'll get a reply...). You may check on Github for addional project contacts.Retentivity
I actually posted a question in the google groups too, no activity activations yet.Irreplaceable
W
0

This might help, and it's from the official spark-java website so it might be considered the right method, or at least the recommended one.

Whitsun answered 31/3, 2017 at 0:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.