Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/test/java/uk/org/webcompere/modelassert/json/ExamplesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,14 @@ void atPath_whenKeysAreMissingInTheActualThenObjectContainsFixesIt() {
.path("z").objectContains()
.isEqualTo("{z:{b:{d:false, c:true}}}");
}

@Test
void at_isCheckedEvenIfTreeComparisonSucceeds() {
assertThatThrownBy(() ->
assertJson("{foo: 42}")
.where().objectContains()
.at("/foo").isText()
.isEqualTo("{}"))
.isInstanceOf(AssertionFailedError.class);
Copy link
Member

@ashleyfrieze ashleyfrieze Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can explain this one. It's sort of user-error, but it's sort of not.

When we use objectContains we're essentially relaxing the rules. We're telling the asserter to ignore any field not present in the expected.

The following version of your test does what your assertion is trying to do:

assertThatThrownBy(() ->
        assertJson("{foo: 42}")
                .where().objectContains()
                .at("/foo").isText()
                .isEqualTo("{foo:42}"))
        .isInstanceOf(AssertionFailedError.class);

}
}