加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – com.github.tomakehurst.wiremock.client.Verificatio

发布时间:2020-12-15 01:09:19 所属栏目:Java 来源:网络整理
导读:我想为API创建一个Stub,并希望验证服务器返回的API调用和响应.因为我已经实现了WireMock示例: import org.junit.Rule;import org.junit.Test;import com.github.tomakehurst.wiremock.junit.WireMockRule;public class MockTestDemo { private static final

我想为API创建一个Stub,并希望验证服务器返回的API调用和响应.因为我已经实现了WireMock示例:

import org.junit.Rule;
import org.junit.Test;

import com.github.tomakehurst.wiremock.junit.WireMockRule;

public class MockTestDemo {

    private static final int WIREMOCK_PORT = 8080;

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(WIREMOCK_PORT);

    @Test
    public void exampleTest() {

    stubFor(get(urlEqualTo("/login")).withHeader("Accept",equalTo("application/json"))
            .willReturn(aResponse().withStatus(200).withBody("Login Success")
                    .withStatusMessage("Everything was just fine!"))
            .willReturn(okJson("{ "message": "Hello" }")));

       verify(getRequestedFor(urlPathEqualTo("http://localhost:8080/login")) 
            .withHeader("Content-Type",equalTo("application/json")));       }

}

但是低于错误:

06001

如果我评论验证部分然后测试执行成功,我也通过调用http:// localhost:8080 / login验证了相同的邮件并且它成功返回响应?

我在这里缺少什么东西?

最佳答案
在您的代码中,您正在查找响应,然后验证是否已为该存根发出请求.但是,您没有调用端点,因此测试失败.

您需要在验证端点之前调用它.

如果您使用Apache Commons HttpClient,您可以将测试编写为:

@Test
public void exampleTest() throws Exception {

    stubFor(get(urlEqualTo("/login")).withHeader("Accept",equalTo("application/json"))
            .willReturn(aResponse().withStatus(200).withBody("Login Success")
                    .withStatusMessage("Everything was just fine!"))
            .willReturn(okJson("{ "message": "Hello" }")));

    String url = "http://localhost:8080/login";
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    request.addHeader("Content-Type","application/json");
    request.addHeader("Accept","application/json");
    HttpResponse response = client.execute(request);

    verify(getRequestedFor(urlPathEqualTo("/login"))
            .withHeader("Content-Type",equalTo("application/json")));
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读