pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.08k stars 480 forks source link

When setting expected response body as PactDslJsonRootValue.numberType(), the verifier is always testing for equality to 100 instead of number type #1435

Open noamaankhan opened 3 years ago

noamaankhan commented 3 years ago

I wrote a Pact consumer contract with the response body expecting a number type using the available api: PactDslJsonRootValue.numberType(). But The verifier is always expecting number 100 instead of just matching a number type.

Here is my example Pact Code:

The consumer pact code:

`@Pact(provider = PROVIDER_NAME, consumer = CONSUMER_NAME) public RequestResponsePact fetchCurrentTimePact(PactDslWithProvider builder) { Map<String, String> responseHeaders = new HashMap<>(); responseHeaders.put(HttpHeaders.CONTENT_TYPE, "text/plain");

    return builder.given("default")
            .uponReceiving("a request to fetch current time")
            .path("/v2/current-time")
            .method("GET")
            .willRespondWith()
            .status(200)
            .headers(responseHeaders)
            .body(PactDslJsonRootValue.numberType())
            .toPact();
}`

I am getting this error when running verifier tests:

java.lang.AssertionError: 0 - BodyComparisonResult(mismatches={/=[BodyMismatch(expected=100, actual=1631595234858, mismatch=Expected body '100' to match '1631595234858' using equality but did not match, path=/, diff=null)]}, diff=[-100, +1631595234858]) at au.com.dius.pact.provider.junit5.PactVerificationContext.verifyInteraction(PactJUnit5VerificationProvider.kt:86)

Can someone please help me with using the right apis. Thank you.

uglyog commented 3 years ago

PactDslJsonRootValue is for JSON values. Use PactDslRootValue for plain text.

Also, as your body is plain text, you can't use type matchers (like numberType), and there are no types with plain text. It is always just text. You probably want PactDslRootValue.stringMatcher('\\d+', '100') which can match the text body with a regex.