1

@Pact(provider="Appointment_Provider",consumer = "Appointment_Consumer") public PactFragment createFragmentAppointmentDetails(PactDslWithProvider 构建器)抛出 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()
                ;

}

4

1 回答 1

1

啊,我想我现在看到了。由于 JsonBody 自己创建了一个对象,因此您需要以2new PactDslJsonBody()结尾closeObject()(因为您在其中打开了另一个对象,所以最后有 2 个)。它应该如下所示:

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();
于 2018-04-18T03:59:22.640 回答