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?
Asked
Active
Viewed 769 times
0
-
I'm sorry this question is not very clear. Are you just asking about how a pact test works with Java? A pact test will always generate a contract file. what's unique to what you're trying to do? – Matthew Fellows Nov 26 '21 at 22:42
-
A pact test will generate a contract file, but request and response we create during writing a pact test with @pact annotation (as shown in the link ) https://docs.pact.io/implementation_guides/jvm/consumer/junit5#2-create-a-method-annotated-with-pact-that-returns-the-interactions-for-the-test Can we write these in JSON file and add this json file to the pact test to generate pact file. Or Any other way to generate a pact file from JSON file which contains(request/response/sate/path) – Srividya Kundeti Nov 27 '21 at 13:29
1 Answers
0
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.

Matthew Fellows
- 3,669
- 1
- 15
- 18
-
Thankyou for your answer. Yes, the question is about generating contracts from JSON files. Do you have any example project in java or either in JS for reference – Srividya Kundeti Nov 29 '21 at 11:50
-
Why do you want to do this? Ideally you want your contract generated from a successful pact test run, so that you know that your client code matches the contract – Timothy Jones Nov 30 '21 at 04:48
-
Yes, you are right, but there are many teams involved and testing their own methods using different tools. So we decided to share a json file which contains the request and expected response from consumer side. We want to generate a pact file from from this JSON file – Srividya Kundeti Nov 30 '21 at 06:43