Search code examples
pactpact-jvmpact-broker

Pact Contract Test :How to generate dynamic PactDslJsonBody using json value?


How to generate dynamic PactDslJsonBody using json value? Is it possible Pact team can provide the auto builder to assign body dynamically?

Pact Body:

body(new PactDslJsonBody()
                .object("testsuite") 
                .stringType("webId","24255")  
                .closeObject());        

Assert Response:

"{\"testsuite\":{\"webId\":\"24255\"}}";

Based on Assert Response(as input) and create the dslbody like

String json = "{\"testsuite\":{\"webId\":\"24255\"}}"

//body(json);
body(generatePactDSLJsonBody(json));        

Assert Response:

assertEqual("{\"testsuite\":{\"webId\":\"24255\"}}",json);

I know in body we can provide json itself. but i need to generate the PactDSLJson body using Json.


Solution

  • It is technically possible to auto-generate the DSL classes from a JSON document, but I do not see the benefit of your example. Most of the time the matchers are defined based on the semantics of the JSON payload, not the syntax.

    For example, from your sample JSON, it would see the webId attribute as a string, and generate a string type matcher. However, it is clearly a number, not a string.

    The auto-generated DSL body would accept the following JSON:

    {"testsuite":{"webId":"This is not a web ID &^*&^%"}}
    

    However, an auto-generation tool used to create a skeleton consumer test from a JSON document which could then be changed based on the semantics of the JSON would be really useful.