0

Using Pack for JVM using Junit5. Maven dependency that I have used is as follows.

<dependency>
  <groupId>au.com.dius.pact.consumer</groupId>
  <artifactId>junit5</artifactId>
  <scope>test</scope>
</dependency>

I am trying to create DslPart response pattern based below JSON payload.

[
   "Africa/Abidjan",
   "Asia/Tokyo",
   "Africa/Addis_Ababa",
   ...
] 

I managed to define the below DslPart definition, which makes the test pass.

   DslPart expectedZoneResponse = new PactDslJsonArray()
            .stringValue("Africa/Abidjan")
            .stringValue("Asia/Tokyo");

Pact definition is.

@Pact(consumer = "Client", provider = "ServiceApi")
public RequestResponsePact getTestArray(PactDslWithProvider builder)
    return builder
           .given("ZoneInfo")
            .uponReceiving("Return all zones.")
              .path("/zones")
              .method("GET")
            .willRespondWith()
              .status(200)
              .body(expectedZoneResponse)
            .toPact();

The above Client test creates the pact definition file but when I run it against my provider contract test, it fails because the response got from server is way more then the static 2 string defined in the pact definition file.

body: $ Expected a List with 2 elements but received 603 elements

My intent was to provide string pattern "[a-zA-Z]+/[a-zA-Z]+" in DslPart expectedZoneResponse, which could be matched, but I could not achieve it.

How can I modify DslPart expectedZoneResponse so that I reflects a patten instead of the 2 string constant?

Thanks,

Gaël J
  • 11,274
  • 4
  • 17
  • 32
MMR
  • 33
  • 7

1 Answers1

0

You can use arrayEachLike or arrayMinLike/arrayMaxLike like this:

DslPart expectedZoneResponse = 
  PactDslJsonArray.arrayEachLike()
    .stringValue("Africa/Abidjan")
    .stringValue("Asia/Tokyo");

See also https://docs.pact.io/implementation_guides/jvm/consumer#matching-json-values-at-the-root

Gaël J
  • 11,274
  • 4
  • 17
  • 32