PACT-JVM problems with closeArray

1k Views Asked by At

So I'm only starting to use JVM-PACT.

I'm trying to crate this JSON

{
  "elements": [
    {
      "Location": {
        "id": "loc_id1",
        "Addr": {
          "Addr1": {
            "value": "545 Test Street"
          },
          "City": {
            "value": "New York City"
          },
          "StateProvCd": {
            "value": "NY"
          },
          "PostalCode": {
            "value": "11111"
          }
        }
      }
    }
  ]
}

Trying to do this: but it keeps on crashing when trying to call closeArray()

DslPart body = new PactDslJsonBody()
            .eachLike("elements")
                .object("Location")                 
                    .stringType("id","loc_id1")                 
                    .object("Addr")                     
                        .object("Addr1")
                            .stringType("value", "545 Test Street")
                        .closeObject()
                        .object("City")
                            .stringType("value", "New York City")
                        .closeObject()
                        .object("StateProvCd")
                            .stringType("value", "NY")
                        .closeObject()
                        .object("PostalCode")
                            .stringType("value", "11111")
                        .closeObject()
                    .closeObject()
                .closeObject()
            .closeArray();

I know it says " can't call closeArray on an Object" but it is definitely trying to close the "eachLike"

java.lang.RuntimeException: Failed to invoke pact method
    at au.com.dius.pact.consumer.PactProviderRule.getPacts(PactProviderRule.java:269)
    at au.com.dius.pact.consumer.PactProviderRule$1.evaluate(PactProviderRule.java:145)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at au.com.dius.pact.consumer.PactProviderRule.getPacts(PactProviderRule.java:267)
    ... 17 more
Caused by: java.lang.UnsupportedOperationException: can't call closeArray on an Object
    at au.com.dius.pact.consumer.dsl.PactDslJsonBody.closeArray(PactDslJsonBody.java:542)

    ... 22 more

Any suggestions would be very helpful

1

There are 1 best solutions below

0
On

Don't you need to close the last object? The one within the array containing location? You're closing the Location ibject, but forgetting that you're not closing the object containing the Location. I know it's a tad confusing with the DSL because arrays opens up a new object, we might have to look into making that better.

DslPart body = new PactDslJsonBody()
            .eachLike("elements")
                // New Object in Array starts here
                .object("Location")                 
                    .stringType("id","loc_id1")                 
                    .object("Addr")                     
                        .object("Addr1")
                            .stringType("value", "545 Test Street")
                            .closeObject()
                        .object("City")
                            .stringType("value", "New York City")
                            .closeObject()
                        .object("StateProvCd")
                            .stringType("value", "NY")
                            .closeObject()
                        .object("PostalCode")
                            .stringType("value", "11111")
                            .closeObject()
                        .closeObject()
                    .closeObject()
                // Array object needs to close here
                .closeObject()
            .closeArray();