Hi! I want to create 2 stubs for the same endpoint...
# help
a
Hi! I want to create 2 stubs for the same endpoint to validate service's behavior. So I decided to filter the incoming requests by a form param:
Copy code
wireMock.stubFor(<http://WireMock.post|WireMock.post>("/convert")
        .withFormParam("amount", WireMock.equalTo("10.50"))
        .willReturn(responseDefinition1));

    wireMock.stubFor(<http://WireMock.post|WireMock.post>("/convert")
        .withFormParam("amount", WireMock.equalTo("12.50"))
        .willReturn(responseDefinition2));
However, the
amount
can easily be
10.5
or
12.5
due to inconsistent precision, and that doesn't work -
10.50
and
10.5
are different strings. I solved this by extending
StringValuePattern
and overriding it's matcher which now converts both values to
BigDecimal
and compares them with
compareTo
. My concern is that I perhaps overengineered this and missed a simpler solution. Is there one to compare decimal values in form params? I only found a similar issue with JSON bodies: https://github.com/wiremock/wiremock/pull/2588
t
I would have suggested using a regex so e.g.
^10\.5[0]?$
a
I was thinking about this, but we handle monetary values as BigDecimals everywhere in the code and generate them randomly. Converting them to regular expressions would create even more overhead, imo. But I guess your answer means that I didn't miss some built-in comparator of form values to decimal types (whether they are float, double, or bigdecimal)?
t
No, there’s nothing else that’s really better suited to this. I guess we should consider adding a matcher or two that do type coercion to numbers in order to handle this kind of thing.
👍 1