6

I'm trying to create a mock server using wire-mock and I'm facing the following problem: I want to hit an URL like this /customers?customerId={customerId}&customerNo={customerNo}.

My question is how can I match the request parameters customerId and customerNo from the stub of the mock server in Java code.

EDIT

After the first response, this is the result:

enter image description here

EDIT 2

Here is my stub:

WireMockServer mockServer = new WireMockServer(8079);
mockServer.start();
mockServer.stubFor(get(urlEqualTo("/api/loan/admin/contracts"))
                .withQueryParam("status", equalTo("ACTIVE"))
                .withQueryParam("cnp", equalTo("1950503410033"))
                .willReturn(aResponse().withBody("Welcome to Baeldung!")));
MrE
  • 19,584
  • 12
  • 87
  • 105
Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56

3 Answers3

2

Query parameters can be passed in URL.

In Java:

urlEqualTo("/your/url?and=query")

Json:

{
  "request": {
    "url": "/your/url?and=query"
    ...
  },
  ...
}

Reference: http://wiremock.org/docs/request-matching/

Example: Try any of the following:

stubFor(any(urlEqualTo("/customers?customerId={your_customer_id}&customerNo={your_customer_no}"))
          .willReturn(aResponse()));


stubFor(any(urlPathEqualTo("/customers"))
          .withQueryParam("customerId", equalTo("your_customer_id"))
          .withQueryParam("customerNo", equalTo("your_customer_no"))
          .willReturn(aResponse()));
Ms. Zia
  • 411
  • 3
  • 12
0

My problem was that the WireMock is hardcoded in UTF-8 format, so when firing requests from browser I didn't send the UTF-8 format to the endpoint. You can't change the WireMock to accept anything instead of UTF-8, but you can fire some UTF-8 requests.

Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56
0

Stubbing with Query Parameters

stubFor(post(urlPathMatching("url"))
.withHeader(CONTENT_TYPE, matching(APPLICATION_JSON_VALUE))
.withQueryParams(queryParams)
.withRequestBody(equalToJson("request-json-goes-here"))
.willReturn(aResponse().withStatus(200)
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.withBody("response-json-goes-here")))

Where query params is

Map<String, StringValuePattern> queryParams = new HashMap<String, StringValuePattern>();
queryParams.put("show_all", equalTo("true"));

Verification of the query parameter can be done using withQueryParam

verify(1, postRequestedFor(urlPathMatching("url"))
.withQueryParam("show_all",equalTo("true")))
Sanjay Bharwani
  • 3,317
  • 34
  • 31