Search code examples
jsonpactpact-broker

Pact Consumer / Provider based in data type and not in data value


We are currently using Pact-Broker in our Spring Boot application with really good results for our integration tests. Our tests using Pact-Broker are base in a call to a REST API and comparing the response with the value in our provider, always using JSON format. Our problem is that the values to compare are in a DB where the data is changing quite often, which make us update the tests really often.

Do you know if it is possible to just validate by the data type?

What we would like to try is to validate that the JSON is properly formed and the data type match, for example, if our REST API gives this output:

[
  {
    "action": "VIEW",
    "id": 1,
    "module": "A",
    "section": "pendingList",
    "state": null
  },
  {
    "action": "VIEW",
    "id": 2,
    "module": "B",
    "section": "finished",
    "state": null
  }
}
]

For example, what we would like to validate from the previous output is the following:

  • The JSON is well formed.
  • All the keys / value pair exists based in the model.
  • The value match a specific data type, for example, that the key action exist in all the entries and contains a string data type.

Do you know if this is possible to be accomplished with Pact-Broker? I was searching in the documentation but I did not found any example of how to do it.

Thanks a lot in advance.

Best regards.


Solution

  • Absolutely! The first 2 things Pact will always do without any extra work.

    What you are talking about is referred to as flexible matching [1]. You don't want to match the value, but the type (or a regex). Given you are using Spring Boot, you may want to look at the various matchers available for Pact JVM [2].

    I'm not sure if you meant it, but just for clarity, Pact and Pact Broker are separate things. Pact is the Open Source contract-testing framework, and Pact Broker [3] is a tool to help share and collaborate on those contracts with the team.