diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index edce9149d..000000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -dist: trusty -language: java -cache: - directories: - - $HOME/.m2 -jdk: - - oraclejdk8 - - openjdk11 - - openjdk14 diff --git a/json-unit-core/pom.xml b/json-unit-core/pom.xml index 760c915eb..b21c3cf9f 100644 --- a/json-unit-core/pom.xml +++ b/json-unit-core/pom.xml @@ -28,9 +28,14 @@ - org.opentest4j - opentest4j - ${opentest4j.version} + org.opentest4j + opentest4j + ${opentest4j.version} + true + + + org.junit.jupiter + junit-jupiter-engine com.fasterxml.jackson.core diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ClassUtils.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ClassUtils.java index e3bf036d1..e50eba0a5 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ClassUtils.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ClassUtils.java @@ -18,9 +18,6 @@ class ClassUtils { /** * Checks if given class is present. - * - * @param className - * @return */ static boolean isClassPresent(String className) { try { diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ExceptionFactory.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ExceptionFactory.java new file mode 100644 index 000000000..2e7dc8962 --- /dev/null +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ExceptionFactory.java @@ -0,0 +1,63 @@ +package net.javacrumbs.jsonunit.core.internal; + +import org.opentest4j.AssertionFailedError; +import org.opentest4j.MultipleFailuresError; + +import java.util.Collections; +import java.util.List; + +import static java.util.stream.Collectors.toList; +import static net.javacrumbs.jsonunit.core.internal.ExceptionUtils.formatDifferences; + +interface ExceptionFactory { + AssertionError createException(String message, Differences diffs); +} + +class BasicExceptionFactory implements ExceptionFactory { + public AssertionError createException(String message, Differences diffs) { + return new BasicJsonAssertError(message, diffs); + } + + private static class BasicJsonAssertError extends AssertionError { + BasicJsonAssertError(String message, Differences differences) { + super(formatDifferences(message, differences)); + } + } +} + +class Opentest4jExceptionFactory implements ExceptionFactory { + @Override + public AssertionError createException(String message, Differences diffs) { + List differences = diffs.getDifferences(); + if (differences.size() == 1) { + JsonDifference difference = differences.get(0); + return new AssertionFailedError(formatDifferences(message, Collections.singletonList(difference)), difference.getExpected(), difference.getActual()); + } else { + return new JsonAssertError(message, diffs); + } + } + + private static class JsonAssertError extends MultipleFailuresError { + private final String message; + private final Differences differences; + + JsonAssertError(String message, Differences differences) { + super(message, differences.getDifferences().stream().map(JsonAssertError::getError).collect(toList())); + this.message = message; + this.differences = differences; + } + + private static AssertionFailedError getError(JsonDifference difference) { + return new AssertionFailedError( + difference.getMessage(), + difference.getExpected().getValue(), + difference.getActual().getValue() + ); + } + + @Override + public String getMessage() { + return formatDifferences(message, differences); + } + } +} diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ExceptionUtils.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ExceptionUtils.java index e514be1bc..fd1064eb5 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ExceptionUtils.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ExceptionUtils.java @@ -15,19 +15,21 @@ */ package net.javacrumbs.jsonunit.core.internal; -import org.opentest4j.AssertionFailedError; - -import java.util.Collections; import java.util.List; +import static net.javacrumbs.jsonunit.core.internal.ClassUtils.isClassPresent; + class ExceptionUtils { private static final String ROOT_MESSAGE = "JSON documents are different:\n"; + private static final ExceptionFactory exceptionFactory + = isClassPresent("org.opentest4j.AssertionFailedError") ? new Opentest4jExceptionFactory() : new BasicExceptionFactory(); + static String formatDifferences(String message, Differences differences) { return formatDifferences(message, differences.getDifferences()); } - private static String formatDifferences(String message, List differences) { + static String formatDifferences(String message, List differences) { StringBuilder builder = new StringBuilder(); if (!differences.isEmpty()) { addHeading(message, builder); @@ -40,13 +42,7 @@ private static String formatDifferences(String message, List dif } static AssertionError createException(String message, Differences diffs) { - List differences = diffs.getDifferences(); - if (differences.size() == 1) { - JsonDifference difference = differences.get(0); - return new AssertionFailedError(formatDifferences(message, Collections.singletonList(difference)), difference.getExpected(), difference.getActual()); - } else { - return new JsonAssertError(message, diffs); - } + return exceptionFactory.createException(message, diffs); } private static void addHeading(String message, StringBuilder builder) { diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/JsonAssertError.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/JsonAssertError.java deleted file mode 100644 index 676711822..000000000 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/JsonAssertError.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2009-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package net.javacrumbs.jsonunit.core.internal; - -import org.opentest4j.MultipleFailuresError; - -import static java.util.stream.Collectors.toList; -import static net.javacrumbs.jsonunit.core.internal.ExceptionUtils.formatDifferences; - -class JsonAssertError extends MultipleFailuresError { - private final String message; - private final Differences differences; - - JsonAssertError(String message, Differences differences) { - super(message, differences.getDifferences().stream().map(JsonDifference::getError).collect(toList())); - this.message = message; - this.differences = differences; - } - - @Override - public String getMessage() { - return formatDifferences(message, differences); - } -} diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/JsonDifference.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/JsonDifference.java index 04acaf4c5..0d84aeadf 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/JsonDifference.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/JsonDifference.java @@ -15,8 +15,6 @@ */ package net.javacrumbs.jsonunit.core.internal; -import org.opentest4j.AssertionFailedError; - class JsonDifference { private final String message; private final Object[] args; @@ -34,10 +32,6 @@ private JsonDifference(String message, Object[] args, Node expected, Node actual this(message, args, context.getExpectedNode(), context.getActualNode()); } - AssertionFailedError getError() { - return new AssertionFailedError(getMessage(), expected.getValue(), actual.getValue()); - } - public Node getExpected() { return expected; } @@ -49,4 +43,4 @@ public Node getActual() { public String getMessage() { return String.format(message, args); } -} \ No newline at end of file +} diff --git a/json-unit-json-path/pom.xml b/json-unit-json-path/pom.xml index 3e22854b7..f0a0439b4 100644 --- a/json-unit-json-path/pom.xml +++ b/json-unit-json-path/pom.xml @@ -34,6 +34,10 @@ ${assertj.version} test + + org.junit.jupiter + junit-jupiter-engine + diff --git a/json-unit-spring/pom.xml b/json-unit-spring/pom.xml index 4e659ea9e..0788464fb 100644 --- a/json-unit-spring/pom.xml +++ b/json-unit-spring/pom.xml @@ -23,6 +23,11 @@ ${project.version} + + org.junit.jupiter + junit-jupiter-engine + + org.jetbrains.kotlin kotlin-stdlib diff --git a/pom.xml b/pom.xml index 2bd049df0..2b56f4837 100644 --- a/pom.xml +++ b/pom.xml @@ -52,10 +52,6 @@ - - org.junit.jupiter - junit-jupiter-engine - org.jetbrains annotations diff --git a/tests/pom.xml b/tests/pom.xml index 40e33febd..f4949f674 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -22,6 +22,7 @@ test-json-path test-no-hamcrest test-kotlin + test-junit4 diff --git a/tests/test-base/src/test/java/net/javacrumbs/jsonunit/test/all/AssumptionsWithAssertJTest.java b/tests/test-base/src/test/java/net/javacrumbs/jsonunit/test/all/AssumptionsWithAssertJTest.java new file mode 100644 index 000000000..75d8e4651 --- /dev/null +++ b/tests/test-base/src/test/java/net/javacrumbs/jsonunit/test/all/AssumptionsWithAssertJTest.java @@ -0,0 +1,11 @@ +package net.javacrumbs.jsonunit.test.all; + +import org.assertj.core.api.Assumptions; +import org.junit.jupiter.api.Test; + +public class AssumptionsWithAssertJTest { + @Test + void shouldBeIgnored() { + Assumptions.assumeThat("Test").isEqualTo("NotEqualToTest"); + } +} diff --git a/tests/test-junit4/pom.xml b/tests/test-junit4/pom.xml new file mode 100644 index 000000000..abc2a1c38 --- /dev/null +++ b/tests/test-junit4/pom.xml @@ -0,0 +1,61 @@ + + + + tests + net.javacrumbs.json-unit + 2.18.2-SNAPSHOT + + 4.0.0 + + test-junit4 + 2.18.2-SNAPSHOT + + + + junit + junit + 4.13 + test + + + org.assertj + assertj-core + ${assertj.version} + test + + + net.javacrumbs.json-unit + json-unit + ${project.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson2.version} + + + ch.qos.logback + logback-classic + 1.2.3 + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + + org.apache.maven.surefire + surefire-junit4 + 3.0.0-M5 + + + + + + diff --git a/tests/test-junit4/src/test/java/net/javacrumbs/jsonunit/test/junit4/AssumptionsWithAssertJTest.java b/tests/test-junit4/src/test/java/net/javacrumbs/jsonunit/test/junit4/AssumptionsWithAssertJTest.java new file mode 100644 index 000000000..ffb5c53e2 --- /dev/null +++ b/tests/test-junit4/src/test/java/net/javacrumbs/jsonunit/test/junit4/AssumptionsWithAssertJTest.java @@ -0,0 +1,11 @@ +package net.javacrumbs.jsonunit.test.junit4; + +import org.assertj.core.api.Assumptions; +import org.junit.Test; + +public class AssumptionsWithAssertJTest { + @Test + public void shouldBeIgnoredButFails() { + Assumptions.assumeThat("Test").isEqualTo("NotEqualToTest"); + } +} diff --git a/tests/test-junit4/src/test/java/net/javacrumbs/jsonunit/test/junit4/JUnit4Test.java b/tests/test-junit4/src/test/java/net/javacrumbs/jsonunit/test/junit4/JUnit4Test.java new file mode 100644 index 000000000..51f7d05c7 --- /dev/null +++ b/tests/test-junit4/src/test/java/net/javacrumbs/jsonunit/test/junit4/JUnit4Test.java @@ -0,0 +1,63 @@ +package net.javacrumbs.jsonunit.test.junit4; + + +import org.junit.Test; + +import static net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class JUnit4Test { + + @Test + public void testComplexErrors() { + assertThatThrownBy(() -> assertJsonEquals("{\n" + + " \"test\":[\n" + + " 1,\n" + + " 2,\n" + + " {\n" + + " \"child\":{\n" + + " \"value1\":1,\n" + + " \"value2\":true,\n" + + " \"value3\":\"test\",\n" + + " \"value4\":{\n" + + " \"leaf\":5\n" + + " }\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"root2\":false,\n" + + " \"root3\":1\n" + + "}", + "{\n" + + " \"test\":[\n" + + " 5,\n" + + " false,\n" + + " {\n" + + " \"child\":{\n" + + " \"value1\":5,\n" + + " \"value2\":\"true\",\n" + + " \"value3\":\"test\",\n" + + " \"value4\":{\n" + + " \"leaf2\":5\n" + + " }\n" + + " },\n" + + " \"child2\":{\n" + + "\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"root4\":\"bar\"\n" + + "}" + )) + .hasMessage("JSON documents are different:\n" + + "Different keys found in node \"\", missing: \"root2\",\"root3\", extra: \"root4\", expected: <{\"root2\":false,\"root3\":1,\"test\":[1, 2, {\"child\":{\"value1\":1,\"value2\":true,\"value3\":\"test\",\"value4\":{\"leaf\":5}}}]}> but was: <{\"root4\":\"bar\",\"test\":[5, false, {\"child\":{\"value1\":5,\"value2\":\"true\",\"value3\":\"test\",\"value4\":{\"leaf2\":5}},\"child2\":{}}]}>\n" + + "Different value found in node \"test[0]\", expected: <1> but was: <5>.\n" + + "Different value found in node \"test[1]\", expected: <2> but was: .\n" + + "Different keys found in node \"test[2]\", extra: \"test[2].child2\", expected: <{\"child\":{\"value1\":1,\"value2\":true,\"value3\":\"test\",\"value4\":{\"leaf\":5}}}> but was: <{\"child\":{\"value1\":5,\"value2\":\"true\",\"value3\":\"test\",\"value4\":{\"leaf2\":5}},\"child2\":{}}>\n" + + "Different value found in node \"test[2].child.value1\", expected: <1> but was: <5>.\n" + + "Different value found in node \"test[2].child.value2\", expected: but was: <\"true\">.\n" + + "Different keys found in node \"test[2].child.value4\", missing: \"test[2].child.value4.leaf\", extra: \"test[2].child.value4.leaf2\", expected: <{\"leaf\":5}> but was: <{\"leaf2\":5}>\n"); + + } + +} diff --git a/tests/test-no-hamcrest/pom.xml b/tests/test-no-hamcrest/pom.xml index e48692710..a6cc4b12e 100644 --- a/tests/test-no-hamcrest/pom.xml +++ b/tests/test-no-hamcrest/pom.xml @@ -32,5 +32,9 @@ org.slf4j slf4j-simple + + org.junit.jupiter + junit-jupiter-engine +