Pact provider verification fails with : For input string: "\null"

1.1k views Asked by At

I am trying to validate on the provider side but getting error -

Verifying a pact between DataConsumer and DataProvider [Using File pact/DataConsumer-DataProvider.json] Given some state a request for json data Request Failed - For input string: "\null"

Not sure what did I miss here.

My Pojo -

@EqualsAndHashCode
@RequiredArgsConstructor
@Builder(toBuilder = true)
@JsonDeserialize(builder = DataModel.DataModelBuilder.class)
public class DataModel {

    @JsonProperty("name")
    private final String name;
    @JsonProperty("price")
    private final double price;
}

Pact -

{
  "provider": {
    "name": "DataProvider"
  },
  "consumer": {
    "name": "DataConsumer"
  },
  "interactions": [
    {
      "description": "a request for json data",
      "request": {
        "method": "GET",
        "path": "/get/ice/2.0"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json; charset\u003dUTF-8"
        },
        "body": {
          "price": 10,
          "name": "some name"
        },
        "matchingRules": {
          "header": {
            "Content-Type": {
              "matchers": [
                {
                  "match": "regex",
                  "regex": "application/json(;\\s?charset\u003d[\\w\\-]+)?"
                }
              ],
              "combine": "AND"
            }
          }
        },
        "generators": {
          "body": {
            "$.name": {
              "type": "ProviderState",
              "expression": "\\${name}",
              "dataType": "STRING"
            },
            "$.price": {
              "type": "ProviderState",
              "expression": "\\${price}",
              "dataType": "FLOAT"
            }
          }
        }
      },
      "providerStates": [
        {
          "name": "some state"
        }
      ]
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "3.0.0"
    },
    "pact-jvm": {
      "version": "3.6.15"
    }
  }
}

Test -


@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Provider("DataProvider")
@PactFolder(value = "pact")
public class ContractVerificationTest {

    @TestTemplate
    @ExtendWith(PactVerificationSpringProvider.class)
    void pactVerificationTestTemplate(PactVerificationContext context) {
        context.verifyInteraction();
    }

    @State("some state")
    void testPact() {
    }
}

Code -

https://github.com/nrworld4/pact-consumer-demo

https://github.com/nrworld4/pact-demo-provider

1

There are 1 answers

3
Matthew Fellows On

You aren't returning the values (name, price) from your provider state annotation in your provider test (it's currently doing nothing) so when Pact tries to replace the values dynamically in the request they are null.

Do you actually need them to be generated by the provider in the first place?

See https://pactflow.io/blog/injecting-values-from-provider-states/ for a detailed example of how to use and fix.

Update Could it be that you're double escaping the parameters?

In the example:

.queryParameterFromProviderState("accountNumber", "\${accountNumber}", "100")

In your code:

.valueFromProviderState("price", "\\${price}", 10.0)