Search code examples
pactpact-jvm

pact matcher for different number of elements in array


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.


Solution

  • 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.