1

Wiremock 的新版本 2.1.7 改变了很多。通过在运行时以编程方式添加存根,以前的一些方法似乎不再适用于直接方法。

例如,我可以像这样在 1.57 版中添加一个存根:

private void setStub(WireMockServer server, String url, RequestMethod requestMethod, Object body)
throws JsonProcessingException {

    UrlMatchingStrategy urlStrategy = new UrlMatchingStrategy();
    urlStrategy.setUrl(url);

    ObjectMapper mapper = new ObjectMapper();
    String jsonInString = mapper.writeValueAsString(body);

    StubMapping mapping = new MappingBuilder(requestMethod, urlStrategy).willReturn(
   new ResponseDefinitionBuilder().withHeader("Access-Control-Allow-Origin", "*").withBody(jsonInString)).build();

    server.addStubMapping(mapping);
}

我怎样才能在 2.1.7 中实现这一点?(是的,我阅读了文档,但我发现只有 JUnit 注释,在我的情况下没有用)。

4

1 回答 1

0

好的,我想我终于想出了如何为 2.1.7 版本编写相同的方法:

private void setStub(WireMockServer server, String url, RequestMethod requestMethod, Object body)
     throws JsonProcessingException {

  ObjectMapper mapper = new ObjectMapper();
  String jsonInString = mapper.writeValueAsString(body);

  server.addStubMapping(
        WireMock.request(requestMethod.getName(), WireMock.urlMatching(url))
              .willReturn(WireMock.aResponse().withStatus(200).withBody(jsonInString)
                    .withHeader("Content-Type", "text/plain").withHeader("Access-Control-Allow-Origin", "*")
                    )
              .build());
}
于 2016-07-19T07:08:45.227 回答