Testing Java Spark Microservices app that implements SparkApplication interface

﹥>﹥吖頭↗ 提交于 2020-01-24 15:14:46

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/37715875/testing-java-spark-microservices-app-that-implements-sparkapplication-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!