Write JSON unit tests in less code. Great for testing REST interfaces.
npx @tessl/cli install tessl/maven-org-skyscreamer--jsonassert@1.5.0A Java library that simplifies JSON unit testing by providing assertion methods that compare JSON strings logically rather than character-by-character. JSONassert offers flexible comparison modes, supports custom value matchers and comparators for advanced validation scenarios, provides detailed error messages that pinpoint specific differences in nested JSON structures, and integrates seamlessly with JUnit testing framework.
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.3</version>
<scope>test</scope>
</dependency>import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;For advanced usage:
import org.skyscreamer.jsonassert.JSONCompare;
import org.skyscreamer.jsonassert.JSONCompareResult;
import org.skyscreamer.jsonassert.comparator.CustomComparator;
import org.skyscreamer.jsonassert.Customization;import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.json.JSONException;
public class MyJsonTest {
@Test
public void testJsonEquality() throws JSONException {
String expected = "{\"name\":\"John\", \"age\":30}";
String actual = "{\"age\":30, \"name\":\"John\"}";
// Lenient comparison - order doesn't matter
JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT);
// Strict comparison - exact match required
JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT);
// Simple boolean mode
JSONAssert.assertEquals(expected, actual, false); // lenient
JSONAssert.assertEquals(expected, actual, true); // strict
}
@Test
public void testJsonArrays() throws JSONException {
String expectedArray = "[{\"id\":1}, {\"id\":2}]";
String actualArray = "[{\"id\":2}, {\"id\":1}]";
// Arrays can be compared in non-strict order
JSONAssert.assertEquals(expectedArray, actualArray, JSONCompareMode.LENIENT);
}
}JSONassert follows a layered architecture with clear separation of concerns:
This design enables flexible testing scenarios from simple equality checks to complex custom validation while maintaining clear error reporting and JUnit integration.
Core assertion functionality providing static methods for comparing JSON strings, objects, and arrays with different comparison modes and detailed error reporting.
public static void assertEquals(String expectedStr, String actualStr, boolean strict) throws JSONException;
public static void assertEquals(String expectedStr, String actualStr, JSONCompareMode compareMode) throws JSONException;
public static void assertEquals(String expectedStr, JSONObject actual, JSONCompareMode compareMode) throws JSONException;
public static void assertEquals(String expectedStr, JSONArray actual, JSONCompareMode compareMode) throws JSONException;
public static void assertNotEquals(String expectedStr, String actualStr, boolean strict) throws JSONException;Low-level comparison functionality that provides programmatic access to JSON comparison results without JUnit integration, useful for custom test frameworks or validation logic.
public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode) throws JSONException;
public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONComparator comparator) throws JSONException;
public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONComparator comparator) throws JSONException;Advanced validation system enabling custom field-level matching with regular expressions, array validation, and path-based customization for complex JSON structures.
public interface ValueMatcher<T> {
boolean equal(T o1, T o2);
}
public class RegularExpressionValueMatcher<T> implements ValueMatcher<T> {
public RegularExpressionValueMatcher(String pattern);
}
public class ArrayValueMatcher<T> implements LocationAwareValueMatcher<T> {
public ArrayValueMatcher(JSONComparator comparator);
public ArrayValueMatcher(JSONComparator comparator, int index);
}Pluggable comparison strategies including default comparison logic, custom field-level matching, and specialized array size comparison for different validation scenarios.
public interface JSONComparator {
JSONCompareResult compareJSON(JSONObject expected, JSONObject actual) throws JSONException;
JSONCompareResult compareJSON(JSONArray expected, JSONArray actual) throws JSONException;
}
public class CustomComparator extends DefaultComparator {
public CustomComparator(JSONCompareMode mode, Customization... customizations);
}public enum JSONCompareMode {
STRICT, // Not extensible, strict array ordering
LENIENT, // Extensible, non-strict array ordering
NON_EXTENSIBLE, // Not extensible, non-strict array ordering
STRICT_ORDER; // Extensible, strict array ordering
public boolean isExtensible();
public boolean hasStrictOrder();
public JSONCompareMode withStrictOrdering(boolean strictOrdering);
public JSONCompareMode withExtensible(boolean extensible);
}
public class JSONCompareResult {
public boolean passed();
public boolean failed();
public String getMessage();
public List<FieldComparisonFailure> getFieldFailures();
public List<FieldComparisonFailure> getFieldMissing();
public List<FieldComparisonFailure> getFieldUnexpected();
public boolean isFailureOnField();
public boolean isMissingOnField();
public boolean isUnexpectedOnField();
public JSONCompareResult fail(String field, Object expected, Object actual);
public JSONCompareResult missing(String field, Object expected);
public JSONCompareResult unexpected(String field, Object actual);
public JSONCompareResult fail(String field, ValueMatcherException exception);
public String toString();
// Deprecated methods
@Deprecated
public Object getActual();
@Deprecated
public Object getExpected();
@Deprecated
public String getField();
}
public class Customization {
public Customization(String path, ValueMatcher<Object> comparator);
public static Customization customization(String path, ValueMatcher<Object> comparator);
public boolean appliesToPath(String path);
public boolean matches(String prefix, Object actual, Object expected, JSONCompareResult result) throws ValueMatcherException;
// Deprecated method
@Deprecated
public boolean matches(Object actual, Object expected);
}
public class FieldComparisonFailure {
public FieldComparisonFailure(String field, Object expected, Object actual);
public String getField();
public Object getExpected();
public Object getActual();
}
public class JSONParser {
/**
* Parse JSON string into JSONObject, JSONArray, or JSONString.
* Returns JSONObject for objects starting with "{",
* JSONArray for arrays starting with "[",
* JSONString for quoted strings or numbers.
*
* @param s Raw JSON string to be parsed
* @return JSONObject, JSONArray, or JSONString instance
* @throws JSONException if string is not valid JSON
*/
public static Object parseJSON(String s) throws JSONException;
}