Skip to content

Commit b85bc4a

Browse files
committed
#560 Add node method to JsonMapAssert
1 parent 6c6f9f1 commit b85bc4a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonMapAssert.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static java.util.Objects.deepEquals;
3333
import static java.util.stream.Collectors.toList;
3434
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.json;
35+
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.getNode;
3536
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.wrapDeserializedObject;
3637
import static org.assertj.core.api.Assertions.entry;
3738
import static org.assertj.core.error.ShouldContain.shouldContain;
@@ -57,6 +58,14 @@ public JsonMapAssert isEqualTo(@Nullable Object expected) {
5758
return compare(expected, configuration);
5859
}
5960

61+
/**
62+
* Moves comparison to given node. Second call navigates from the last position in the JSON.
63+
*/
64+
@NotNull
65+
public JsonAssert node(@NotNull String node) {
66+
return new JsonAssert(path.to(node), configuration, getNode(actual, node));
67+
}
68+
6069
@Override
6170
@NotNull
6271
public JsonMapAssert containsValue(@Nullable Object expected) {

tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,18 @@ void shouldAssertInteger() {
736736
assertThatJson("{\"a\":-10}").node("a").isIntegralNumber();
737737
}
738738

739+
@Test
740+
void shouldAllowNodeInJsonMapAssert() {
741+
assertThatThrownBy(() ->
742+
assertThatJson("{\"data\":{\"id\": \"1234\", \"relationships\": false}}")
743+
.inPath("$.data")
744+
.isObject()
745+
.containsEntry("id", "1234")
746+
.node("relationships")
747+
.isObject()
748+
).hasMessage("Node \"$.data.relationships\" has invalid type, expected: <object> but was: <false>.");
749+
}
750+
739751
@Test
740752
void shouldAssertIntegerFailure() {
741753
assertThatThrownBy(() -> assertThatJson("{\"a\":1.0}").node("a").isIntegralNumber())

0 commit comments

Comments
 (0)