troubleshooting Question

Java Test failing

Avatar of ltpitt
ltpitt asked on
JavaJSON
1 Comment1 Solution39 ViewsLast Modified:
Hello there,

I have the following Java test in my Spring Cloud Contract Producer:

DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).array("['content']").field("['idA']").field("['value']").isEqualTo("IDA1");
assertThatJson(parsedJson).array("['content']").field("['idA']").field("['value']").isEqualTo("IDA2");
assertThatJson(parsedJson).array("['content']").field("['idA']").field("['value']").isNull();
assertThatJson(parsedJson).array("['content']").field("['idA']").field("['value']").isNull();
assertThatJson(parsedJson).array("['content']").field("['idB']").field("['value']").isEqualTo("IDB1");
assertThatJson(parsedJson).array("['content']").field("['idB']").field("['value']").isEqualTo("IDB2");
assertThatJson(parsedJson).array("['content']").field("['idB']").field("['value']").isEqualTo("IDB3");
assertThatJson(parsedJson).array("['content']").field("['idB']").field("['value']").isEqualTo("IDB4");
assertThatJson(parsedJson).field("['size']").isEqualTo(4);

When I run it I get the following error:
java.lang.IllegalStateException: Parsed JSON [{"content":[{"idA":{"value":"IDA1"},"idB":{"value":"IDB1"}},{"idA":{"value":"IDA2"},"idB":{"value":"IDB2"}},{"idA":{"value":"IDA3"},"idB":null},{"idA":{"value":"IDA4"},"idB":null}],"size":4}] doesn't match the JSON path [$.['content'][*].['idB'][?(@.['value'] == null)]]

If I remove the rows checking the null values the error is gone so the error is related to the null checks.

What am I doing wrong?
ASKER CERTIFIED SOLUTION
ste5an
Senior Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros