单元测试Java Spark微框架

mel*_*r55 3 java rest junit unit-testing spark-java

我正在尝试为java微服务添加一个restful api.为此,我使用spark:

http://sparkjava.com/documentation.html

我创建了一个非常简单的类,它代表了api.那堂课在这里:

public class Routes {
    public void establishRoutes(){
        get("/test", (req, res) -> "Hello World");
        after((req, res) -> {
            res.type("application/json");
        });

        exception(IllegalArgumentException.class, (e, req, res) -> {
            res.status(400);
        });
    }
Run Code Online (Sandbox Code Playgroud)

现在,运行Routes.establishRoutes()应该站起来,如果有人决定访问http:// localhost:4567/test,它将显示"Hello World" .这确实有效.欢呼!

下一步是对代码进行单元测试.不幸的是,我的单元测试没有成功.spark文档没有详细说明进行测试的合理方式,因此我所拥有的内容是根据我在网络上找到的示例拼凑而成的.这是我的Junit测试:

public class TestRoutes {
        @Before
        public void setUp() throws Exception {
            Routes newRoutes = new Routes();
            newRoutes.establishRoutes();
        }

        @After
        public void tearDown() throws Exception {
            stop();
        }

        @Test
        public void testModelObjectsPOST(){

            String testUrl = "/test";


            ApiTestUtils.TestResponse res = ApiTestUtils.request("GET", testUrl, null);
            Map<String, String> json = res.json();
            assertEquals(201, res.status);
        }
Run Code Online (Sandbox Code Playgroud)

以下是ApiTestUtils.request()背后的代码:

public class ApiTestUtils {
    public static TestResponse request(String method, String path, String requestBody) {


        try {
            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);
        } catch (IOException e) {
            e.printStackTrace();
            fail("Sending request failed: " + e.getMessage());
            return null;
        }
    }

    public static class TestResponse {

        public final String body;
        public final int status;

        public TestResponse(int status, String body) {
            this.status = status;
            this.body = body;
        }

        public Map<String,String> json() {
            return new Gson().fromJson(body, HashMap.class);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在connection.connect()里面失败了ApiTestUtils.request().具体来说,我收到错误:java.lang.AssertionError: Sending request failed: Connection refused

我相信这种情况正在发生,因为当我的测试尝试发出请求时应用程序没有监听.但是,我不明白为什么会这样.我从这里的演示项目中借用了测试代码:https: //github.com/mscharhag/blog-examples/blob/master/sparkdemo/src/test/java/com/mscharhag/sparkdemo/UserControllerIntegrationTest.java

更新: 我尝试运行上面链接的示例.事实证明,它也不起作用.看起来在这种情况下启动一个火花实例比我想象的更困难?我不是想弄明白怎么做.

mar*_*neo 7

在您的测试用例中缺少用于等待嵌入式服务器初始化的代码.我已经尝试过您的代码,并且遇到了与您相同的问题,但在调试之后我注意到嵌入式spark服务器在新创建的线程中初始化.(见方法spark.Service#init()).您在测试中需要做的就是通过调用方法等待初始化spark.Spark#awaitInitialization()

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
import static spark.Spark.awaitInitialization;
import static spark.Spark.stop;

public class TestRoutes {
    @Before
    public void setUp() throws Exception {
        Routes newRoutes = new Routes();
        newRoutes.establishRoutes();

        awaitInitialization();

    }

    @After
    public void tearDown() throws Exception {
        stop();
    }

    @Test
    public void testModelObjectsPOST() {

        String testUrl = "/test";


        ApiTestUtils.TestResponse res = ApiTestUtils.request("GET", testUrl, null);
        assertEquals(200, res.status);
    }

}
Run Code Online (Sandbox Code Playgroud)