HI Guys, suppose 1.there is contract signe...
# pact-jvm
s
HI Guys, suppose 1.there is contract signed between consumer and provider (for data type and attribute values) 2. I need to do the pact verification by giving all the mandatory values(for data type and attribute values) as positive scenario(200) 3. and again I need to do the pact verification between same counsumer and provider by missing some mandatory values(for data type and attribute values) as negative scenario(400) so i am currently able to do the verification for statement 2nd and 3rd by using seperate pact class. but i don't want diffrent classes for pact file creation. i want this verification for positive and negative under the single class. so is this possible to do validation of positive and negative under single pact class?
m
thanks for raising here. What is currently stopping you from doing this in a single pact class?
(the answer is yes)
b
Hey @saraswati Vitthal Pawar, here's an example, maybe that helps. https://github.com/basdijkstra/introduction-to-contract-testing/tree/main/customer-consumer/src/test/java/customer If you checkout the entire repository and run the tests for any of the consumers through
mvn clean test
in the correct subfolder, you'll see that all interactions, from both test classes, end up in the same contract. Off topic: both 'positive' and 'negative' are included, although I'd suggest reframing your thinking around this, it is all behaviour 🙂. See https://www.ontestautomation.com/lets-talk-about-behaviour-instead/ for my thoughts on this.
💯 1
s
I am trying below code for generating pact file but only one detest() is getting passed that is for negative one and for another one which is positive that is failing. but when i try to run the positive one by commenting negative it is getting passed what could be the reason ? public class XYZ_PostTest extends CommonModules { static CommonModules common = new CommonModules(); public static final String providerServiceName = "XYZ_provider"; public static final String consumerServiceName = "XYZ_Consumer"; public static int providerServicePort = 8110; public static String providerUrl = "http://localhost:" + providerServicePort + "/xyzbasepath"; public String postbody1positive = new String(Files.readAllBytes(Paths.get("src/Resources/XYZ.txt")), StandardCharsets.UTF_8); public String postbody2negative = new String(Files.readAllBytes(Paths.get("src/Resources/LMN.txt")), StandardCharsets.UTF_8); public XYZ_PostTest() throws IOException { } @Rule public PactProviderRuleMk2 provider = new PactProviderRuleMk2(providerServiceName, "localhost", providerServicePort, this); @Pact(consumer = consumerServiceName) public RequestResponsePact createPact(PactDslWithProvider builder) { Map<String, String> reqheaders = new HashMap(); reqheaders.put("Content-Type", "application/json"); // adding required headers under this Map<String, String> resheaders = new HashMap(); resheaders.put("Content-Type", "application/json"); //adding required response headers under this DslPart reqBodywithoutmandatoryfiels = new PactDslJsonBody() .stringType("Uuid", common.randomUUID()) // adding reqBody under this without mandatory fields for negative validation with error code 400 DslPart resBody = new PactDslJsonBody() // adding responsebody for the 400 error code .asBody(); return builder .given("publishe sevent details without mandatory fields") .uponReceiving("sends acknowledgement for bad request") .method("POST") .headers(reqheaders) .body(reqBodywithoutmandatoryfiels) .path("/xyzbasepath") .willRespondWith() .status(400) .headers(resheaders) .body(resBody) .toPact(); } @Pact(consumer = consumerServiceName) public RequestResponsePact createPactpositivescenario(PactDslWithProvider builder) { System.out.println("JWT Token: "+JWTtoken); Map<String, String> reqheaders1 = new HashMap(); reqheaders1.put("Content-Type", "application/json"); // adding required headers under this Map<String, String> resheaders1 = new HashMap(); resheaders1.put("Content-Type", "application/json"); //adding required response headers under this DslPart reqBodyWithmandatoryFields1 = new PactDslJsonBody() .stringType("locationTypeCode","COUNTRY") //adding reqBody under this with mandatory fields for positive validation with success code 200 .asBody(); DslPart resBody1 = new PactDslJsonBody() // adding responsebody for the 202 success code return builder .given("publishesevent details with mandatory fields") .uponReceiving("sends acknowledgement for success") .method("POST") .headers(reqheaders1) .body(reqBodyWithmandatoryFields1) .path("/xyzbasepath") .willRespondWith() .status(202) .headers(resheaders1) .body(resBody1) .toPact(); } @Test @Category(Create_Location_C_ORS_Consumer.class) @PactVerification() public void doTest1() throws IOException { HttpPost httpPost = null; HttpClient httpClient = new DefaultHttpClient(); String url=String.format(providerUrl); HttpEntity httpEntity = new StringEntity((postbody2negative), "utf-8"); httpPost = new HttpPost(url); httpPost.setHeader("Content-type", "application/json"); // added required headers httpPost.setEntity(httpEntity); HttpResponse httpResponse = httpClient.execute(httpPost); } @Test @Category(Create_Location_C_ORS_Consumer.class) @PactVerification() public void doTest2() throws IOException { HttpPost httpPost = null; HttpClient httpClient = new DefaultHttpClient(); String url=String.format(providerUrl); HttpEntity httpEntity = new StringEntity((postbody1positive), "utf-8"); httpPost = new HttpPost(url); httpPost.setHeader("Content-type", "application/json"); //added required headers httpPost.setEntity(httpEntity); HttpResponse httpResponse = httpClient.execute(httpPost); } }
b
There's a lot going on here, but afaict, this doesn't even compile.
It also looks like you're blending consumer and provider DSLs.
On your builder chains, it looks like you're trying to set multiple batches of headers and bodies, which probably won't work as you expect.
I've not seen pact-jvm tests that really look like this before. Do you have a link to an article or course content that gives examples like this?
🤔 1
Another aside: you can format your code (using triple backticks), which will make it easier for everyone to read 🙂
☝️ 1
m
You would normally have one set of tests for your Consumer tests, which would reside in the API client’s project / code base. You would have an entirely separate set of Provider Verification, which would reside in the API proider’s project/code base. Having them in the same code base is not strictly uncommon (e.g. in the case of a monorepo) but having them in the same class is definitely not a good idea