Java 如何在版本2.1.7中向Wiremock服务器添加stubMapping?
Wiremock的新版本2.1.7发生了很大变化。通过在运行时以编程方式添加存根,以前的一些方法似乎不再适用于直接方法 例如,我可以在版本1.57中添加一个存根,如下所示:Java 如何在版本2.1.7中向Wiremock服务器添加stubMapping?,java,rest,junit,wiremock,Java,Rest,Junit,Wiremock,Wiremock的新版本2.1.7发生了很大变化。通过在运行时以编程方式添加存根,以前的一些方法似乎不再适用于直接方法 例如,我可以在版本1.57中添加一个存根,如下所示: private void setStub(WireMockServer server, String url, RequestMethod requestMethod, Object body) throws JsonProcessingException { UrlMatchingStrategy urlStra
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注释,在我的情况下,这些注释没有用。)好的,我想我终于找到了为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());
}
好的,我想我终于找到了为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());
}
使用静态导入,而不是说
WireMock.aResponse()
,等等。为此,请将代码更改为aResponse()
,然后在导入时执行静态导入。它大大缩短了代码,使其更具可读性。我需要一个“独立”版本,而不是Junit测试。因此,在这种情况下,使用静态导入是没有用的。请使用静态导入,而不是说WireMock.aResponse()
,等等。为此,请将代码更改为aResponse()
,然后在导入时,执行静态导入。它大大缩短了代码,使其更具可读性。我需要一个“独立”版本,而不是Junit测试。因此,在这种情况下,使用静态导入是没有用的。