Search code examples
pactpact-jvm

How to return json array with PactDslJsonArray in consumer tests?


I'm trying to write a consumer test and the following json array will be my response.

[
  {
    "additionalInfo": {
      "details": {
        "logo": "German Logo"
      }
    },
    "flagRequired": true,
    "name": "Germany"
  },
  {
    "additionalInfo": {
      "details": {
        "logo": "Dutch Logo"
      }
    },
    "flagRequired": true,
    "name": "Netherlands"
  },
  ...
]

I tried to create a pact like below.

val responseBody = PactDslJsonArray().`object`()
                .stringType("name")
                .booleanType("flagRequired")
                .`object`("additionalInfo")
                .`object`("details")
                .stringType("logo")

When I run the provider test, it throws the following error message.

1.1) body: $ Expected a List with 1 elements but received 8 elements

What change I need to make in PactDslJsonArray part to return it multiple times? Thank you in advance.

Pact Plugin Version: 4.3.19


Solution

  • You will want to use the "like" versions of this for the root object. See https://docs.pact.io/implementation_guides/jvm/consumer#root-level-arrays-that-match-all-items

    e.g.

    PactDslJsonArray.arrayEachLike()
                      .stringType("name")
                      .booleanType("flagRequired")
                      .`object`("additionalInfo")
                      .`object`("details")
                      .stringType("logo")
    .closeObject()