测试实现SparkApplication接口的Java Spark Microservices应用程序 [英] Testing Java Spark Microservices app that implements SparkApplication interface

查看:185
本文介绍了测试实现SparkApplication接口的Java Spark Microservices应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我遇到了这个资源但是,我无法按照我的预期工作。还有这个资源 Java中的示例Spark github 但他们都可能使用嵌入式服务器。

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!"); 
    }
}

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

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

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

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


推荐答案

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

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.

这篇关于测试实现SparkApplication接口的Java Spark Microservices应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆