1

@Pact(provider="Appointment_Provider",consumer = "Appointment_Consumer") public PactFragment createFragmentAppointmentDetails (PactDslWithProvider builder) throws ParseException{

        Map<String, String> headers = new HashMap<>(); 
        headers.put("Content-Type", "application/json"); 


        return builder
                .given("GetAppoinment")
                .uponReceiving("Get Appointment information")
                .path("/getappointment")
                .query("apptId=11207")
                .method("GET")
                .willRespondWith()
                .headers(headers)
                .status(200)
                .body(new PactDslJsonBody()
                        .object("appointments")
                        .stringValue("type","Test \\u0026 Turn up")
                        .stringValue("apptId","11207")
                        .closeObject()
                        )
                .toFragment()
                ;

}

J_A_X
  • 12,857
  • 1
  • 25
  • 31
Nisha
  • 11
  • 4
  • java.lang.RuntimeException: Failed to invoke pact method at au.com.dius.pact.consumer.PactProviderRule.getPacts(PactProviderRule.java:269) at au.com.dius.pact.consumer.PactProviderRule$1.evaluate(PactProviderRule.java:145) Caused by: java.lang.NoClassDefFoundError: org/json/JSONObject – Nisha Apr 10 '18 at 18:21
  • Sounds like you're missing a dependency - can you post your dependencies? – Timothy Jones Apr 10 '18 at 19:24
  • @Nisha Could you please provide more context and maybe a better example that includes everything relative to Pact? It does sounds like you're missing a dependency, how are you adding the Pact dependency to your project? Can you show us the file? – J_A_X Apr 11 '18 at 05:54
  • These are two pact dependencies added au.com.dius pact-jvm-consumer-junit_2.11 3.5.4 org.codehaus.groovy groovy-all au.com.dius pact-jvm-provider-junit_2.11 3.5.4 – Nisha Apr 11 '18 at 14:40

1 Answers1

1

Ah, I think I see it now. You need to end new PactDslJsonBody() with closeObject() (so there's 2 in the end since you opened another object within it) since the JsonBody creates an object itself. It should look like this:

builder
    .given("GetAppoinment")
    .uponReceiving("Get Appointment information")
    .path("/getappointment")
    .query("apptId=11207")
    .method("GET")
    .willRespondWith()
    .headers(headers)
    .status(200)
    .body(
            new PactDslJsonBody() // This opens a root object
                .object("appointments") // creates a new object with in the root one
                    .stringValue("type","Test \\u0026 Turn up")
                    .stringValue("apptId","11207")
                .closeObject() // closes the child object
           .closeObject() // closes the root object
    )
    .toFragment();
J_A_X
  • 12,857
  • 1
  • 25
  • 31