WireMock:存根 - 无法正确实施 "AssertThat" 方法

WireMock: Stubbing - Not able to implement "AssertThat" method correctly

我正在努力创建一个简单的存根。我一直在网上关注 wiremock 教程,但在我面临的问题上没有成功。特别是,AssertMethod 中的 "Get" 方法给我带来了很多问题。不知道怎么解决。

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 方法带有红色下划线。

请帮忙!

WireMock documentation 并没有很好地概述如何与之建立联系(但是,在他们的辩护中,这并不真正在他们的蛋糕中)。

正如我在评论中所回避的那样,HttpResponse 不包含 GET() 方法,这就是为什么您得到 "red underline"(IE:错误)的原因。

所以我们知道我们正在寻找针对状态代码的断言。如果我们查看 class 的 Javadoc of the HttpResponse class, there is the StatusLine class that can be retrieved from getStatusLine() of HttpResponse, and the Javadoc 表明它包含一个 getStatusCode() 方法。将此信息合并到您的答案中,断言需要更新为:

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

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

我对这个问题的贡献:这个 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();
        }


    }


}