Skip to content

Commit

Permalink
#820 Fix Hamcrest matcher error message
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Oct 10, 2024
1 parent 3b31eb6 commit 61d53f3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ void matchHamcrestMatcher(Context context, Node actualNode, Matcher patternMatch
Object value = actualNode.getValue();
if (!matcher.matches(value)) {
Description description = new StringDescription();
description.appendText("Expected ");
matcher.describeTo(description);
description.appendText(" but ");
matcher.describeMismatch(value, description);
valueDifferenceReporter.differenceFound(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void useMatcher() {
.node("result.decimal")
.isEqualTo("${json-unit.matches:negative}")))
.hasMessage(
"JSON documents are different:\nMatcher \"negative\" does not match value 1.00001 in node \"result.decimal\". <1.00001> was greater than <0>\n");
"JSON documents are different:\nMatcher \"negative\" does not match value 1.00001 in node \"result.decimal\". Expected a value less than <0> but <1.00001> was greater than <0>\n");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ void ifMatcherDoesNotMatchReportDifference() {
.withDifferenceListener(listener)
.isEqualTo("{\"test\": \"${json-unit.matches:positive}\"}"))
.hasMessage(
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". <-1> was less than <0>\n");
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". Expected a value greater than <0> but <-1> was less than <0>\n");

assertThat(listener.getDifferenceList()).hasSize(1);
assertThat(listener.getDifferenceList().get(0).toString())
Expand All @@ -1327,7 +1327,7 @@ void parametrizedMatcherShouldFail() {
.isEqualTo("{\"test\": \"${json-unit.matches:isDivisibleBy}3\"}");
} catch (AssertionError e) {
assertEquals(
"JSON documents are different:\nMatcher \"isDivisibleBy\" does not match value 5 in node \"test\". It is not divisible by <3>\n",
"JSON documents are different:\nMatcher \"isDivisibleBy\" does not match value 5 in node \"test\". Expected value divisible by <3> but was <5>\n",
e.getMessage());
}
}
Expand Down Expand Up @@ -2311,6 +2311,28 @@ void shouldFailFast() {
""");
}

@Test
void hamcrestMessageTest() {
assertThatThrownBy(
() -> assertThatJson("""
{
"someText": "abc123"
}
""")
.withMatcher("exampleMatcher", org.hamcrest.Matchers.equalTo("def456"))
.isEqualTo(
"""
{
"someText": "${json-unit.matches:exampleMatcher}"
}
"""))
.hasMessage(
"""
JSON documents are different:
Matcher "exampleMatcher" does not match value "abc123" in node "someText". Expected "def456" but was "abc123"
""");
}

private static final String json =
"""
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ void parametrizedMatcherShouldFail() {
JsonAssert.withMatcher("isDivisibleBy", divisionMatcher));
} catch (AssertionError e) {
assertEquals(
"JSON documents are different:\nMatcher \"isDivisibleBy\" does not match value 5 in node \"test\". It is not divisible by <3>\n",
"JSON documents are different:\nMatcher \"isDivisibleBy\" does not match value 5 in node \"test\". Expected value divisible by <3> but was <5>\n",
e.getMessage());
}
}
Expand Down Expand Up @@ -1102,7 +1102,7 @@ void parameterMatchingShouldWork() {
"""
JSON documents are different:
Different value found when comparing expected array element test[0] to actual element test[1].
Matcher "eq" does not match value 2 in node "test[1]".\s
Matcher "eq" does not match value 2 in node "test[1]". Expected number equal to <1> but was <2>
""");
}

Expand All @@ -1116,12 +1116,7 @@ public boolean matches(Object item) {

@Override
public void describeTo(Description description) {
description.appendValue(param);
}

@Override
public void describeMismatch(Object item, Description description) {
description.appendText("It is not divisible by ").appendValue(param);
description.appendText("value divisible by ").appendValue(param);
}

@Override
Expand Down Expand Up @@ -1163,12 +1158,9 @@ public boolean matches(Object item) {

@Override
public void describeTo(Description description) {
description.appendValue(param);
description.appendText("number equal to ").appendValue(param);
}

@Override
public void describeMismatch(Object item, Description description) {}

@Override
public void setParameter(String parameter) {
this.param = new BigDecimal(parameter);
Expand Down Expand Up @@ -1295,7 +1287,7 @@ void ifMatcherDoesNotMatchReportDifference() {
"{\"test\":-1}",
withMatcher("positive", greaterThan(valueOf(0)))))
.hasMessage(
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". <-1> was less than <0>\n");
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". Expected a value greater than <0> but <-1> was less than <0>\n");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ void ifMatcherDoesNotMatchReportDifference() {
.withDifferenceListener(listener)
.isEqualTo("{\"test\": \"#{json-unit.matches:positive}\"}"))
.hasMessage(
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". <-1> was less than <0>\n");
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". Expected a value greater than <0> but <-1> was less than <0>\n");
assertThat(listener.getDifferenceList()).hasSize(1);
assertThat(listener.getDifferenceList().get(0).toString())
.isEqualTo("DIFFERENT Expected #{json-unit.matches:positive} in test got -1 in test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void ifMatcherDoesNotMatchReportDifference() {
Expected: {"test": "${json-unit.matches:positive}"}
but: JSON documents are different:
Matcher "positive" does not match value -1 in node "test". <-1> was less than <0>
Matcher "positive" does not match value -1 in node "test". Expected a value greater than <0> but <-1> was less than <0>
""");
}

Expand Down

0 comments on commit 61d53f3

Please sign in to comment.