2

在旧版本的 Wiremock 中,我们可以将 te queryString 附加到匹配的 url,如下所示:

wireMockRule.stubFor(get(urlPathEqualTo("/rest/customer/find?custId=1&custId=2")) //
                .willReturn(aResponse()) //

但是现在我们升级到最新版本,我们必须使用映射构建器的 withQueryParam 方法——wiremock 不再理解附加的参数。但是我们如何实现这样的行为呢?我不知道如何在存根中注入多值参数。

Integer[] custIds = {1, 2};
wireMockRule.stubFor(get(urlPathEqualTo("/rest/customer/find")) //
                .withQueryParam("userIds", equalTo(custIds.toString())) //
                .willReturn(aResponse()) //

当我们从 restTemplate 调用路径为“/rest/customer/find?custId=1&custId=2”的 url 时,这不起作用。

4

2 回答 2

1

你可以试试:

stubFor(get(urlEqualTo("/rest/user/list?userIds=5&userIds=6&userIds=7&userIds=8"))

或者

stubFor(get(urlPathEqualTo("/rest/user/list"))
                .withQueryParam("userIds", equalTo("5"))
                .withQueryParam("userIds", equalTo("6"))
                .withQueryParam("userIds", equalTo("7"))
                .withQueryParam("userIds", equalTo("8"))
于 2021-03-11T09:31:18.210 回答
-1

尝试在 url 中附加查询参数会导致此错误:

2016-12-07 10:31:49.205 ERROR 1835 --- [qtp846274760-33] WireMock                                 SFID= : Request was not matched:
{
  "url" : "/rest/user/list?userIds=5&userIds=6&userIds=7&userIds=8",
  "absoluteUrl" : "http://localhost:8889/rest/user/list?userIds=5&userIds=6&userIds=7&userIds=8",
  "method" : "GET",
  "clientIp" : "127.0.0.1",
  "headers" : {
    "User-Agent" : "Java/1.8.0_91",
    "Connection" : "keep-alive",
    "Host" : "localhost:8889",
    "Accept" : "application/json, application/*+json"
  },
  "cookies" : { },
  "browserProxyRequest" : false,
  "loggedDate" : 1481113909179,
  "bodyAsBase64" : "",
  "body" : "",
  "loggedDateString" : "2016-12-07T12:31:49Z"
}
Closest match:
{
  "url" : "http://localhost:8889/rest/user/list?userIds=5&userIds=6&userIds=7&userIds=8",
  "method" : "GET"
}
于 2016-12-07T16:57:32.073 回答