From 52a03a293f5fa4ca589ddd84edf8ef95f5f069cf Mon Sep 17 00:00:00 2001 From: Valery Yatsynovich Date: Thu, 2 Apr 2020 15:24:43 +0300 Subject: [PATCH] Do not append tolerance with zero value to diff message --- .../net/javacrumbs/jsonunit/core/internal/Diff.java | 11 +++++++++-- .../jsonunit/test/base/AbstractJsonAssertTest.java | 8 ++++++++ .../test/base/AbstractJsonFluentAssertTest.java | 6 ++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Diff.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Diff.java index 6b54867d7..bf6c05dd2 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Diff.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Diff.java @@ -23,6 +23,7 @@ import java.math.BigDecimal; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -329,8 +330,14 @@ private void compareNodes(Context context) { if (configuration.getTolerance() != null && !hasOption(context.getActualPath(), IGNORING_VALUES)) { BigDecimal diff = expectedValue.subtract(actualValue).abs(); if (diff.compareTo(configuration.getTolerance()) > 0) { - reportValueDifference(context, "Different value found in node \"%s\", " + differenceString() + ", difference is %s, tolerance is %s", - fieldPath, quoteTextValue(expectedValue), quoteTextValue(actualValue), diff.toString(), configuration.getTolerance()); + List arguments = new ArrayList<>(Arrays.asList(fieldPath, quoteTextValue(expectedValue), quoteTextValue(actualValue), diff.toString())); + String message = "Different value found in node \"%s\", " + differenceString() + ", difference is %s"; + if (configuration.getTolerance().compareTo(BigDecimal.ZERO) != 0) + { + arguments.add(configuration.getTolerance()); + message += ", tolerance is %s"; + } + reportValueDifference(context, message, arguments.toArray()); } } else { compareValues(context, expectedValue, actualValue); 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 fd10e29bc..2aff5698d 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 @@ -176,6 +176,14 @@ void testComparisonWhenOverTolerance() { "Different value found in node \"\", expected: <1> but was: <1.1>, difference is 0.1, tolerance is 0.01\n"); } + @Test + void testComparisonWhenOverZeroTolerance() { + setTolerance(0); + assertThatThrownBy(() -> assertJsonEquals("1", "\n1.1\n")) + .hasMessage("JSON documents are different:\n" + + "Different value found in node \"\", expected: <1> but was: <1.1>, difference is 0.1\n"); + } + @Test void testNullOk() { assertJsonEquals("{\"test\":null}", "{\n\"test\": null\n}"); 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 6d76d2caf..7f3593de9 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 @@ -124,6 +124,12 @@ void testAssertToleranceFailure() { .hasMessage("JSON documents are different:\nDifferent value found in node \"test\", expected: <1> but was: <1.1>, difference is 0.1, tolerance is 0.001\n"); } + @Test + void testAssertZeroToleranceFailure() { + assertThatThrownBy(() -> assertThatJson("{\"test\":1.1}").node("test").withTolerance(0.0).isEqualTo(1)) + .hasMessage("JSON documents are different:\nDifferent value found in node \"test\", expected: <1> but was: <1.1>, difference is 0.1\n"); + } + @Test void testAssertNode() { assertThatThrownBy(() -> assertThatJson(readValue("{\"test\":1}")).isEqualTo(readValue("{\"test\":2}")))