CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-skyscreamer--jsonassert

Write JSON unit tests in less code. Great for testing REST interfaces.

Pending
Overview
Eval results
Files

assertions.mddocs/

JSON Assertions

Core assertion functionality providing static methods for comparing JSON strings, objects, and arrays with different comparison modes and detailed error reporting. These methods integrate directly with JUnit and throw AssertionError on comparison failures.

Capabilities

String-to-String Assertions

Compare JSON strings directly with flexible comparison modes and optional custom error messages.

public static void assertEquals(String expectedStr, String actualStr, boolean strict) throws JSONException;
public static void assertEquals(String message, 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 message, String expectedStr, String actualStr, JSONCompareMode compareMode) throws JSONException;
public static void assertEquals(String expectedStr, String actualStr, JSONComparator comparator) throws JSONException;
public static void assertEquals(String message, String expectedStr, String actualStr, JSONComparator comparator) throws JSONException;

Usage Examples:

// Basic strict comparison
JSONAssert.assertEquals("{\"name\":\"John\"}", "{\"name\":\"John\"}", true);

// Lenient comparison with different field order
JSONAssert.assertEquals("{\"name\":\"John\",\"age\":30}", "{\"age\":30,\"name\":\"John\"}", false);

// Custom error message
JSONAssert.assertEquals("User data should match", expected, actual, JSONCompareMode.LENIENT);

// Using comparison modes explicitly
JSONAssert.assertEquals(expected, actual, JSONCompareMode.NON_EXTENSIBLE);

String-to-JSONObject Assertions

Compare JSON strings against JSONObject instances, useful when working with pre-parsed JSON objects.

public static void assertEquals(String expectedStr, JSONObject actual, boolean strict) throws JSONException;
public static void assertEquals(String message, String expectedStr, JSONObject actual, boolean strict) throws JSONException;
public static void assertEquals(String expectedStr, JSONObject actual, JSONCompareMode compareMode) throws JSONException;
public static void assertEquals(String message, String expectedStr, JSONObject actual, JSONCompareMode compareMode) throws JSONException;

Usage Examples:

JSONObject actualObject = new JSONObject("{\"name\":\"John\",\"age\":30}");

// Compare string to object
JSONAssert.assertEquals("{\"name\":\"John\"}", actualObject, JSONCompareMode.LENIENT);

// With custom message
JSONAssert.assertEquals("Object comparison failed", "{\"name\":\"John\"}", actualObject, true);

String-to-JSONArray Assertions

Compare JSON strings against JSONArray instances for array-specific validation scenarios.

public static void assertEquals(String expectedStr, JSONArray actual, boolean strict) throws JSONException;
public static void assertEquals(String message, String expectedStr, JSONArray actual, boolean strict) throws JSONException;
public static void assertEquals(String expectedStr, JSONArray actual, JSONCompareMode compareMode) throws JSONException;
public static void assertEquals(String message, String expectedStr, JSONArray actual, JSONCompareMode compareMode) throws JSONException;

Usage Examples:

JSONArray actualArray = new JSONArray("[{\"id\":1},{\"id\":2}]");

// Compare string to array with different ordering
JSONAssert.assertEquals("[{\"id\":2},{\"id\":1}]", actualArray, JSONCompareMode.LENIENT);

// Strict array ordering
JSONAssert.assertEquals("[{\"id\":1},{\"id\":2}]", actualArray, JSONCompareMode.STRICT_ORDER);

JSONObject-to-JSONObject Assertions

Direct comparison between JSONObject instances with support for custom comparators.

public static void assertEquals(JSONObject expected, JSONObject actual, boolean strict) throws JSONException;
public static void assertEquals(String message, JSONObject expected, JSONObject actual, boolean strict) throws JSONException;
public static void assertEquals(JSONObject expected, JSONObject actual, JSONCompareMode compareMode) throws JSONException;
public static void assertEquals(String message, JSONObject expected, JSONObject actual, JSONCompareMode compareMode) throws JSONException;
public static void assertEquals(JSONObject expected, JSONObject actual, JSONComparator comparator) throws JSONException;
public static void assertEquals(String message, JSONObject expected, JSONObject actual, JSONComparator comparator) throws JSONException;

JSONArray-to-JSONArray Assertions

Direct comparison between JSONArray instances with flexible ordering and element matching.

public static void assertEquals(JSONArray expected, JSONArray actual, boolean strict) throws JSONException;
public static void assertEquals(String message, JSONArray expected, JSONArray actual, boolean strict) throws JSONException;
public static void assertEquals(JSONArray expected, JSONArray actual, JSONCompareMode compareMode) throws JSONException;
public static void assertEquals(String message, JSONArray expected, JSONArray actual, JSONCompareMode compareMode) throws JSONException;

Negative Assertions

All assertion methods have corresponding assertNotEquals variants that verify JSON structures do NOT match.

public static void assertNotEquals(String expectedStr, String actualStr, boolean strict) throws JSONException;
public static void assertNotEquals(String message, String expectedStr, String actualStr, boolean strict) throws JSONException;
public static void assertNotEquals(String expectedStr, String actualStr, JSONCompareMode compareMode) throws JSONException;
public static void assertNotEquals(String message, String expectedStr, String actualStr, JSONCompareMode compareMode) throws JSONException;
public static void assertNotEquals(String expectedStr, String actualStr, JSONComparator comparator) throws JSONException;
public static void assertNotEquals(String message, String expectedStr, String actualStr, JSONComparator comparator) throws JSONException;
public static void assertNotEquals(JSONObject expected, JSONObject actual, boolean strict) throws JSONException;
public static void assertNotEquals(String message, JSONObject expected, JSONObject actual, boolean strict) throws JSONException;
public static void assertNotEquals(JSONObject expected, JSONObject actual, JSONCompareMode compareMode) throws JSONException;
public static void assertNotEquals(String message, JSONObject expected, JSONObject actual, JSONCompareMode compareMode) throws JSONException;
public static void assertNotEquals(JSONObject expected, JSONObject actual, JSONComparator comparator) throws JSONException;
public static void assertNotEquals(String message, JSONObject expected, JSONObject actual, JSONComparator comparator) throws JSONException;
public static void assertNotEquals(JSONArray expected, JSONArray actual, boolean strict) throws JSONException;
public static void assertNotEquals(String message, JSONArray expected, JSONArray actual, boolean strict) throws JSONException;
public static void assertNotEquals(JSONArray expected, JSONArray actual, JSONCompareMode compareMode) throws JSONException;
public static void assertNotEquals(String message, JSONArray expected, JSONArray actual, JSONCompareMode compareMode) throws JSONException;

Usage Examples:

// Verify JSON structures are different
JSONAssert.assertNotEquals("{\"status\":\"active\"}", "{\"status\":\"inactive\"}", false);

// Verify arrays don't match
JSONAssert.assertNotEquals("[1,2,3]", "[1,2,4]", JSONCompareMode.STRICT);

Error Handling

All assertion methods throw JSONException for JSON parsing errors and AssertionError for comparison failures. The AssertionError messages include detailed information about where differences were found:

// Example failure message:
// name
// Expected: John
//      got: Jane

Comparison Mode Behavior

  • STRICT: Exact match required, no additional fields allowed, array order matters
  • LENIENT: Additional fields allowed, array order doesn't matter
  • NON_EXTENSIBLE: No additional fields allowed, array order doesn't matter
  • STRICT_ORDER: Additional fields allowed, array order matters

Install with Tessl CLI

npx tessl i tessl/maven-org-skyscreamer--jsonassert

docs

assertions.md

comparators.md

comparison.md

custom-matchers.md

index.md

tile.json