簡體   English   中英

使用PlayFramework / JUnit測試HTTP響應

[英]Test HTTP response with PlayFramework/JUnit

我一直在尋找幾個小時來尋找一種方法來測試比賽路線是否正確。
我的意思是,例如

localhost:9000/test的GET請求是否實際上返回200 OK

我找不到任何有效的示例。

我使用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);的調用assertThat(wsResponse1.getStatus()).isEqualTo(OK);

希望能有所幫助。 如果您需要進一步說明,請發表評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM