PactDslJsonBody无法创建JSON对象

时间:2018-04-10 18:20:04

标签: testing integration pact pact-jvm

@Pact(provider =“Appointment_Provider”,consumer =“Appointment_Consumer”)     public PactFragment createFragmentAppointmentDetails(PactDslWithProvider builder)抛出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()
                ;

}

1 个答案:

答案 0 :(得分:1)

啊,我想我现在看到了。您需要使用new PactDslJsonBody()结束closeObject()(因此在您打开其中的另一个对象时最终会有2个),因为JsonBody会自己创建一个对象。它应该是这样的:

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();