WireMock Optional QueryParams

1,811 views
Skip to first unread message

c.be...@gmail.com

unread,
Nov 14, 2017, 9:03:27 AM11/14/17
to wiremock-user
Have a question, working on some spring contract tests using wire mock.  I'm testing a call that has optional request params, I want my stub to reflect this, so that if I test against the stub I can leave out the optional params and it will still be valid.  Here is an example of what I have to date:

```java
MvcResult result = mockMvc
                .perform(get(BASE_URL + "example?startDateTime=2012-10-01T09:45Z&endDateTime=2012-10-01T09:45:00Z"+ "&type=NBQ&pageNumber=0&pageLimit=10").accept(MediaType.APPLICATION_JSON_VALUE))

                .andExpect(status().isOk()).andDo(WireMockRestDocs.verify()
                        .wiremock(WireMock.get(urlPathMatching(BASE_URL + "example"))
                        .withQueryParam("startDateTime", WireMock.matching(ISO8601_FORMAT))
.withQueryParam("endDateTime", WireMock.matching(ISO8601_FORMAT))
.withQueryParam("type", WireMock.matching(".*"))
.withQueryParam("pageNumber", WireMock.matching("[0-9]*"))
.withQueryParam("pageLimit", WireMock.matching("[0-9]*"))
                        ).stub("view/getOneEvent"))
                .andReturn();
```

In the above example startDateTime is the only required param needed.  so I would like the rest of the queryParams to be optional.  I tried adding the regex "?" but I think it might be the param key that is causing the issue.

Is there an easy way in this case to have my queryParams as optional?  it would perhaps be good to have the key as a pattern matcher? (or perhaps i'm going about this totally the wrong way)

Tom Akehurst

unread,
Nov 14, 2017, 9:24:54 AM11/14/17
to wiremock-user
At the moment there isn't a way to say "either absent, or present and matching this", which I think is what you're after.

The workaround would be to create several stubs that represent the different combinations of parameters you expect to test with. A little bit duplicative unfortunately, but should work OK.
Message has been deleted

c.be...@gmail.com

unread,
Nov 14, 2017, 9:29:37 AM11/14/17
to wiremock-user
Yeah that is exactly what I'm after, no worries I can create multiple stubs.

Thanks for the quick response

Hossein Shemshadi

unread,
Jul 21, 2018, 2:15:54 AM7/21/18
to wiremock-user
Hi Tom
I wonder if this issue was done on wiremock or not
do we still must create multiple stubs for not required queryParams?
I searched a lot for this need but no one answered this. Thanks for your answer :)

Tom Akehurst

unread,
Jul 21, 2018, 4:20:44 PM7/21/18
to wiremock-user
No, nothing has changed on this front so multiple stubs is still the way to go.
Reply all
Reply to author
Forward
0 new messages