Search code examples
pact

Pact Testing - Json Request body with multiple key value pair in object, but keys can be anything


I have a json request body as below -

{"attributes":{"key1":"abc","key2":"xyz"}} - not just limiting to 2 key-value pairs. can be any number (1 or more)

key1, key2 - can be any string value. i.e - In one request key1 can be "Age", in Next request - key1 can be "Name".

How to generate PactDslJsonBody for this request.

My code -

PactDslJsonBody dsl = new PactDslJsonBody();
        dsl.object("attributes")
        .stringType("key1")
        .closeObject();

this creates a matching rule -

"$.attributes.key1": {

but i want it to handle any number of key value pair and also key can be any string

something like - "$.attributes.*": {

If above scenario is not supported, is there way I can check if object is not null and has some value?


Solution

  • Please see eachKeyLike: https://docs.pact.io/implementation_guides/jvm/consumer/#matching-any-key-in-a-map

    This ignores the name of the key, but specifies the type of value it should have.