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 479 forks source link

Check numberType and decimalType #1614

Open louisrj opened 2 years ago

louisrj commented 2 years ago

Below is a code snippet of a consumer pact test written with pact-jvm version 4.2.14 This test is used to verify the json structure of a message published to a kafka topic. When the pact is verified by the provider, it also compares for the values of the two fields decimalField1 ( default value 100.0) and numberType(default value of 100). How can I use pact to only verify if decimalField1 is decimal data type and numberField1 is number data type without matching the values?

Consumer Test Class:

@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(
    providerName = "example-provider",
    providerType = ProviderType.ASYNCH)
class Test {

  @Pact(provider = "example-provider", consumer = "example-consumer")
  MessagePact createPact(MessagePactBuilder builder) {
    DslPart dslPart =
        LambdaDsl.newJsonBody(
                body ->
                    body
                        .decimalType("decimalField1")
                        .numberType("numberField1"))
            .build();

    var metadata = new HashMap<String, String>();
    metadata.put("Topic", "TestTopic");
    metadata.put(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);

    return builder
        .hasPactWith("testTopic")
        .withMetadata(metadata)
        .withContent(body)
        .toPact();
  }

Generated pact json:

{
  "consumer": {
    "name": "test-consumer"
  },
  "messages": [
    {
      "contents": {
        "decimalField1": 100.0,
        "numberField1": 100
      },
      "generators": {
        "body": {
          "$.decimalField1": {
            "digits": 10,
            "type": "RandomDecimal"
          },
          "$.numberField1": {
            "max": 2147483647,
            "min": 0,
            "type": "RandomInt"
          }
        }
      },
      "matchingRules": {
        "body": {
          "$.decimalField1": {
            "combine": "AND",
            "matchers": [
              {
                "match": "decimal"
              }
            ]
          },
          "$.numberField1": {
            "combine": "AND",
            "matchers": [
              {
                "match": "number"
              }
            ]
          }
        }
      },
      "metaData": {
        "Topic": "TestTopic",
        "contentType": "application/json"
      }
    }
  ],
  "metadata": {
    "pact-jvm": {
      "version": "4.2.14"
    },
    "pactSpecification": {
      "version": "3.0.0"
    }
  },
  "provider": {
    "name": "test-provider"
  }
}

Failure output:

Description of differences
------------------------------------------------
    • Expected 100.0 but got 5.99 at $.decimalField1
    • Expected 100 but got 83 at $.decimalField1
rholshausen commented 2 years ago

How is the pact is verified by the provider? I don't recognise that failure output.