pactpact-jvmpact-java

Is there a way to add pact interaction into to JSON file and add to consumer test code in JAVA


We have a requirement that we need have a pact standalone server and interactions (request/ expected response pairs) in a JSON files and store it in a directory and just add to consumer tests and generate a pact file. I would like to know if this possible in PACT using java?if yes, Could you please also provide an example?


Solution

  • I think the question is about generating contracts from JSON files.

    See this note about generating contracts: https://docs.pact.io/faq/#can-i-generate-my-pact-file-from-something-like-swagger.

    You can definitely read in JSON documents in Java and convert to the matching DSL.

    It's easier in languages like JS because JSON is more native, but ultimately you need to be careful about things like getting the matching rules right otherwise you'll create very brittle contracts that are hard to verify on the provider side.

    See also https://docs.pact.io/consumer which gives you advice around writing good consumer tests.