测试实现SparkApplication接口的

时间:2016-06-09 02:26:25

标签: unit-testing junit java-8 microservices spark-java

我正在试图弄清楚如何测试用Java Spark编写的网络/休息服务,并且没有很多教程如何做到这一点。由于Apache SparkJava Spark之间的混淆而找到答案很棘手。

我偶然发现了resource但是,我无法按照我的预期工作。 Java Spark resource中还有github和示例,但它们都可能使用嵌入式服务器。

无论如何,假设我有以下服务

public class RestService implements SparkApplication {
    @Override
    public void init() {
        get("/", (req, res) -> "Hello World!"); 
    }
}

我想测试以上内容,以确保Hello World!请求HTTP GET。我有以下测试:

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;
        }
    }
}

运行上面的测试,测试成功执行,输出如下所示,但我主要关心的是这是否是测试Java Spark webapp的正确方法,目的是在嵌入式服务器上运行(当SparkApplication实现时)而且init()被覆盖了?)

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 个答案:

答案 0 :(得分:0)

This可能会有所帮助,而且它来自官方的spark-java网站,所以它可能被认为是正确的方法,或者至少是推荐的方法。