From cc01111c9a998a6f2a2396e4bd30be220b7eb80f Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Thu, 10 Oct 2024 20:26:43 +0200 Subject: [PATCH] #820 Fix Hamcrest matcher error message --- .../core/internal/HamcrestHandler.java | 3 +++ .../spring/testit/WebTestClientTest.java | 2 +- .../test/base/AbstractAssertJTest.java | 26 +++++++++++++++++-- .../test/base/AbstractJsonAssertTest.java | 18 ++++--------- .../base/AbstractJsonFluentAssertTest.java | 2 +- .../test/base/AbstractJsonMatchersTest.java | 2 +- 6 files changed, 35 insertions(+), 18 deletions(-) diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/HamcrestHandler.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/HamcrestHandler.java index 9e494510..1f31c463 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/HamcrestHandler.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/HamcrestHandler.java @@ -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, diff --git a/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/WebTestClientTest.java b/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/WebTestClientTest.java index 4ff47f75..2de9fa18 100644 --- a/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/WebTestClientTest.java +++ b/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/WebTestClientTest.java @@ -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 diff --git a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java index 2bdf99e8..d8661989 100644 --- a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java +++ b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java @@ -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()) @@ -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()); } } @@ -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 = """ { diff --git a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonAssertTest.java b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonAssertTest.java index 4fcf84b6..a7c71604 100644 --- a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonAssertTest.java +++ b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonAssertTest.java @@ -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()); } } @@ -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> """); } @@ -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 @@ -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); @@ -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 diff --git a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonFluentAssertTest.java b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonFluentAssertTest.java index 624fe788..c73242d9 100644 --- a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonFluentAssertTest.java +++ b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonFluentAssertTest.java @@ -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"); diff --git a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonMatchersTest.java b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonMatchersTest.java index 557cb8f1..2a92dbad 100644 --- a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonMatchersTest.java +++ b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractJsonMatchersTest.java @@ -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> """); }