From 00831a52a7e7cbca855a903c66beb85050f233f0 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 +++ .../test/base/AbstractAssertJTest.java | 26 +++++++++++++++++-- .../test/base/AbstractJsonAssertTest.java | 15 +++++------ .../base/AbstractJsonFluentAssertTest.java | 2 +- .../test/base/AbstractJsonMatchersTest.java | 2 +- 5 files changed, 35 insertions(+), 13 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/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..7ee4763c 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 it is not divisible by <3>\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..e7524dd3 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 it is not divisible by <3>\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,12 @@ public boolean matches(Object item) { @Override public void describeTo(Description description) { - description.appendValue(param); + description.appendText("value divisible by ").appendValue(param); } @Override public void describeMismatch(Object item, Description description) { - description.appendText("It is not divisible by ").appendValue(param); + description.appendText("it is not divisible by ").appendValue(param); } @Override @@ -1163,12 +1163,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 +1292,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> """); }