Skip to content

Commit

Permalink
Merge pull request #277 from lukas-krecan/opentest4j
Browse files Browse the repository at this point in the history
#276 Make opentest4j optional
  • Loading branch information
lukas-krecan authored Sep 5, 2020
2 parents e33ed81 + cc28ef0 commit 8b4551c
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 74 deletions.
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

11 changes: 8 additions & 3 deletions json-unit-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@

<dependencies>
<dependency>
<groupId>org.opentest4j</groupId>
<artifactId>opentest4j</artifactId>
<version>${opentest4j.version}</version>
<groupId>org.opentest4j</groupId>
<artifactId>opentest4j</artifactId>
<version>${opentest4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
class ClassUtils {
/**
* Checks if given class is present.
*
* @param className
* @return
*/
static boolean isClassPresent(String className) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<JsonDifference> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonDifference> differences) {
static String formatDifferences(String message, List<JsonDifference> differences) {
StringBuilder builder = new StringBuilder();
if (!differences.isEmpty()) {
addHeading(message, builder);
Expand All @@ -40,13 +42,7 @@ private static String formatDifferences(String message, List<JsonDifference> dif
}

static AssertionError createException(String message, Differences diffs) {
List<JsonDifference> 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) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package net.javacrumbs.jsonunit.core.internal;

import org.opentest4j.AssertionFailedError;

class JsonDifference {
private final String message;
private final Object[] args;
Expand All @@ -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;
}
Expand All @@ -49,4 +43,4 @@ public Node getActual() {
public String getMessage() {
return String.format(message, args);
}
}
}
4 changes: 4 additions & 0 deletions json-unit-json-path/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 5 additions & 0 deletions json-unit-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
Expand Down
4 changes: 0 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@
</modules>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
Expand Down
1 change: 1 addition & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>test-json-path</module>
<module>test-no-hamcrest</module>
<module>test-kotlin</module>
<module>test-junit4</module>
</modules>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
61 changes: 61 additions & 0 deletions tests/test-junit4/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tests</artifactId>
<groupId>net.javacrumbs.json-unit</groupId>
<version>2.18.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>test-junit4</artifactId>
<version>2.18.2-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson2.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>3.0.0-M5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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: <false>.\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: <true> 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");

}

}
Loading

0 comments on commit 8b4551c

Please sign in to comment.