1

I have an endpoint which returns following type of response:

"0": {
  "array": [
    "items",
    2,
    "item1",
    "item2"
  ]
}

Currently I have written pact matcher like following:

o.object("0", o1 -> {
  o1.array("array", a1 -> {
    a1.stringValue("items");
    a1.numberType(2);
    a1.stringType("item1");
    a1.stringType("item2");
  });
});

Now this only works if provider returns exact number of elements in the array. It doesn't work if provider doesn't return one of the item, for example:

"0": {
  "array": [
    "items",
    1,
    "item1"
  ]
}

Or if provider retuns an extra item:

"0": {
  "array": [
    "items",
    3,
    "item1",
    "item2",
    "item3"
  ]
}

1st element is fixed, 2nd element mentions how many items are returned and then follows as many elements as items.

How can I write matcher for this? Thanks.

keeping_it_simple
  • 439
  • 1
  • 11
  • 31

1 Answers1

1

There is a new array contains matcher that might be helpful https://docs.pact.io/implementation_guides/jvm/consumer/#array-contains-matcher-v4-specification, it lets you specfiy heterogeneous elements of an array.

The structure you have provided with the specific semantics will be hard to test this way though.

Matthew Fellows
  • 3,669
  • 1
  • 15
  • 18
  • Thanks, I tried arrayContaining() method instead of array() in my above mentioned code but it gives me following error: 01 Mar 2021 16:20:51,400 DEBUG ArrayContainsJsonGenerator:43 [HTTP-Dispatcher] Comparing variant 0 with value 'items' 01 Mar 2021 16:20:51,401 ERROR BaseJdkMockServer:235 [HTTP-Dispatcher] Failed to generate response au.com.dius.pact.core.model.InvalidPathExpression: Path expression "[0]" does not start with a root marker "$" – keeping_it_simple Mar 01 '21 at 16:39
  • It sounds like you might have the matcher at the wrong level, so you may need to provide an "update" to your answer so we can diagnose. But I'd suggest joining us at slack.pact.io or raising a reproduceable issue on the Pact JVM repo. – Matthew Fellows Mar 02 '21 at 11:22
  • Thanks, I have raised an issue in pact jvm repo: https://github.com/pact-foundation/pact-jvm/issues/1318 – keeping_it_simple Mar 03 '21 at 10:47