Avatar of ltpitt
ltpitt

asked on 

Java Test failing

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);

Open in new window


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)]]

Open in new window


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?
JavaJSON

Avatar of undefined
Last Comment
ste5an
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo