WireMock:Stubbing - 无法实现" AssertThat"方法正确

时间:2018-04-24 11:43:50

标签: java wiremock

我正在努力创建一个简单的存根。我一直在线跟踪线索,但在面临的问题上没有成功。特别是" Get" AssertMethod中的方法给了我很多问题。我不知道如何解决它。

public class WeatherApplicationTest {


    @Rule
    public WireMockRule wireMockRule = new WireMockRule();
    public  WireMockServer wireMockServer = new WireMockServer(); //No-args constructor will start on port 8080, no HTTPS



    @BeforeClass
    public  void setUpClass() {
        wireMockServer.start();
    }

    @AfterClass
    public  void tearDownClass() {
        wireMockServer.stop();
    }


    @Test
    public void statusMessage() throws IOException{

        wireMockRule.stubFor(get(urlEqualTo("/some/thing"))
        .willReturn(aResponse()
        .withStatus(200)
        .withStatusMessage("Everything is fine")
        .withHeader("Content-Type", "Text/Plain")));

        HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://localhost:" + wireMockServer.port() + "/some/thing");
        HttpResponse response = client.execute(request);

        assertThat(response.GET("/some/thing").statusCode(), is(200));


}
}

我在以下一行收到错误:

    assertThat(response.GET("/some/thing").statusCode(), is(200));

GET方法在Red中加下划线。

请帮助!

2 个答案:

答案 0 :(得分:0)

WireMock documentation并没有很好地概述如何与它联系(但是,在他们的辩护中,它并没有真正在他们的片中)。

正如我在评论中提到的那样,HttpResponse不包含GET()方法,这就是为什么您获得红色下划线" (IE:错误)。

所以我们知道我们正在寻求对状态代码进行断言。如果我们查看HttpResponse类的Javadoc,可以从StatusLine getStatusLine()和{{3}中检索HttpResponse类该类显示它包含getStatusCode()方法。将这些信息结合到您的答案中,需要将断言更新为:

assertThat(response.getStatusLine().getStatusCode(), is(200));

此外,正如Tom在评论中指出的那样,删除WireMockServer(以及start/stopgetPort()来电;您可以从规则中获取该端口。应该静态调用stubFor(...)方法(不作为规则的一部分)。

答案 1 :(得分:0)

我对这个问题的贡献:此Java测试在IDE和Maven中都无例外地运行,并且与其他测试一起运行,因为最后,它关闭了模拟服务器,并且没有关闭与在其他测试中运行的其他服务器发生任何冲突。

import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import wiremock.org.apache.http.HttpResponse;
import wiremock.org.apache.http.client.HttpClient;
import wiremock.org.apache.http.client.methods.HttpGet;
import wiremock.org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;

import static com.github.tomakehurst.wiremock.client.WireMock.*;


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class KatharsisControllerTest {

    private static WireMockServer mockedServer;
    private static HttpClient client;

    @BeforeClass
    public static void setUpClass() {

        mockedServer = new WireMockServer();
        mockedServer.start();
        client = new DefaultHttpClient();

    }

    @AfterClass
    public static void tearDown() {
        mockedServer.stop();
    }

    @Test
    public void test_get_all_mock() {


        stubFor(get(urlEqualTo("/api/Payment"))
                .willReturn(aResponse()
                        .withHeader("Content-Type", "application/vnd.api+json;charset=UTF-8")
                        .withStatus(200)
                        .withBody("")


                ));


        HttpGet request = new HttpGet("http://localhost:8080/api/Payment");
        try {
            HttpResponse response = client.execute(request);
            assert response.getStatusLine().getStatusCode() == 200;

        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}