【问题标题】:Test HTTP response with PlayFramework/JUnit使用 PlayFramework/JUnit 测试 HTTP 响应
【发布时间】:2015-03-17 14:21:17
【问题描述】:

我一直在寻找一种方法来测试路线是否正确。
我的意思是,例如

localhost:9000/test 的GET 请求是否真的返回200 OK

我找不到任何工作示例。

【问题讨论】:

    标签: unit-testing junit playframework playframework-2.2 playframework-2.3


    【解决方案1】:

    我使用 Play 的 WS(Web 服务)库来实现这一点。例如:

    package endpoints;
    
    import com.fasterxml.jackson.databind.JsonNode;
    import models.Meh;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import play.libs.Json;
    import play.libs.ws.WS;
    import play.libs.ws.WSResponse;
    import play.test.TestServer;
    
    import static org.fest.assertions.Assertions.assertThat;
    import static play.test.Helpers.*;
    
    public class MehEndpointTest {
    
        private static long timeout;
        private static Meh meh1;
        private static Meh meh2;
        // Test Server
        private static TestServer testServer = testServer(3333, fakeApplication(inMemoryDatabase()));
        private static JsonNode meh1Node;
        private static JsonNode meh2Node;
    
        @BeforeClass
        public static void setUp() {
            timeout = 10000L;
            // Dummy Objects
            meh1 = new Meh();
            meh1.meh = "foo";
            meh1.yo = "bar";
            meh2 = new Meh();
            meh2.meh = "hell";
            meh2.yo = "world";
            meh1Node = Json.toJson(meh1);
            meh2Node = Json.toJson(meh2);
            // Start the server
            start(testServer);
        }
    
        @Test
        public void testAdd() {
            WSResponse wsResponse1 = WS.url("http://localhost:3333/api/meh").post(meh1Node).get(timeout);
            WSResponse wsResponse2 = WS.url("http://localhost:3333/api/meh").post(meh2Node).get(timeout);
            JsonNode jsonNode1 = wsResponse1.asJson();
            JsonNode jsonNode2 = wsResponse2.asJson();
            assertThat(wsResponse1.getStatus()).isEqualTo(CREATED);
            assertThat(wsResponse2.getStatus()).isEqualTo(CREATED);
            assertThat(jsonNode1.isObject()).isEqualTo(true);
            assertThat(jsonNode1.get("id").asLong()).isEqualTo(1L);
            assertThat(jsonNode2.isObject()).isEqualTo(true);
            assertThat(jsonNode2.get("id").asLong()).isEqualTo(2L);
        }
    
        @Test
        public void testFind() {
            WSResponse wsResponse1 = WS.url("http://localhost:3333/api/meh").get().get(timeout);
            WSResponse wsResponse2 = WS.url("http://localhost:3333/api/meh/2").get().get(timeout);
            JsonNode jsonNode1 = wsResponse1.asJson();
            JsonNode jsonNode2 = wsResponse2.asJson();
            assertThat(wsResponse1.getStatus()).isEqualTo(OK);
            assertThat(wsResponse2.getStatus()).isEqualTo(OK);
            assertThat(jsonNode1.isArray()).isEqualTo(true);
            assertThat(jsonNode2.isObject()).isEqualTo(true);
            assertThat(jsonNode1).hasSize(2);
            assertThat(jsonNode2.get("id").asLong()).isEqualTo(2);
        }
    
        @Test
        public void testUpdate() {
            meh1.id = 1L;
            meh1.yo = "changed yo";
            WSResponse wsResponse1 = WS.url("http://localhost:3333/api/meh/1").put(Json.toJson(meh1)).get(timeout);
            JsonNode jsonNode1 = wsResponse1.asJson();
            assertThat(wsResponse1.getStatus()).isEqualTo(OK);
            assertThat(jsonNode1.isObject()).isEqualTo(true);
            assertThat(jsonNode1.get("id").asLong()).isEqualTo(1);
            assertThat(jsonNode1.get("yo").asText()).isEqualTo("changed yo");
        }
    
        @Test
        public void testDelete() {
            WSResponse wsResponse1 = WS.url("http://localhost:3333/api/meh").post(meh1Node).get(timeout);
            WSResponse wsResponse2 = WS.url("http://localhost:3333/api/meh/3").delete().get(timeout);
            WSResponse wsResponse3 = WS.url("http://localhost:3333/api/meh").get().get(timeout);
            JsonNode jsonNode1 = wsResponse3.asJson();
            assertThat(wsResponse1.getStatus()).isEqualTo(CREATED);
            assertThat(wsResponse2.getStatus()).isEqualTo(OK);
            assertThat(wsResponse3.getStatus()).isEqualTo(OK);
            assertThat(jsonNode1).hasSize(2);
        }
    
        @AfterClass
        public static void tearDown() {
            // Stop the server
            stop(testServer);
        }
    }
    

    注意对assertThat(wsResponse1.getStatus()).isEqualTo(OK);的调用

    希望对您有所帮助。如果您需要进一步说明,请发表评论。

    【讨论】:

    • 哇。非常感谢。只是想知道一件事:我的服务器实际上是在 localhost:9000 上运行的(所以 Ws.url("localhost:9000")。testServer 第一个参数中的 3333 是什么?
    • 它是假应用程序/服务器将使用的端口号,因此您可以根据需要将其更改为 9000。但是,如果您需要在测试的同时运行应用程序,那么您当然需要使它们不同以避免“地址正在使用”错误。
    • 那么,我不需要运行我的 localhost:9000 服务器来执行测试?
    • 不,无论端口号如何,路由都将保持不变。因此,如果您的常规应用程序中有一个路由 localhost:9000/test,那么测试服务器中就有一个相应的路由 localhost:3333/test
    • 功能强大。谢谢大家的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    相关资源
    最近更新 更多