JSONassert Documentation

repository·master·Indexed 21 days ago

https://github.com/skyscreamer/jsonassert

A Java library for writing concise JSON unit tests. JSONassert compares JSON strings and objects by evaluating their logical structure rather than literal string comparisons, offering both strict and non-strict modes to handle field ordering and additional data.

Tokens
691
Snippets
2
Records
4
Agent score
27%

What's inside JSONassert

  1. Understand JSONassert strict mode

    master

    JSONassert provides two modes of comparison via the strictMode boolean parameter in assertEquals:

    1. Non-strict mode (strictMode = false): This is the recommended setting for most tests. It compares the logical structure and data. It is less brittle because it ignores the order of elements in arrays or objects and allows the actual JSON to contain additional fields not present in the expected string.
    2. Strict mode (strictMode = true): This mode requires the actual JSON to match the expected JSON exactly in structure and content, typically requiring identical ordering and no extra fields.
  2. Install JSONassert via Maven

    master

    To add JSONassert to your Java project, include the following dependency in your pom.xml. Note that the scope is set to test as it is primarily used for unit testing.

    <dependency>
        <groupId>org.skyscreamer</groupId>
        <artifactId>jsonassert</artifactId>
        <version>2.0-rc1</version>
        <scope>test</scope>
    </dependency>
  3. Interpret JSONassert error messages

    master

    When an assertion fails, JSONassert produces descriptive error messages that pinpoint exactly where the mismatch occurred in the JSON hierarchy. This is particularly useful for deep or long JSON structures.

    Example Error Format: friends[id=3].pets[]: Expected bird, but not found ; friends[id=3].pets[]: Contains cat, but not expected

    This indicates that within the friends array, the object with id=3 had a pets array that was missing the expected value bird and instead contained cat.

  4. Use JSONAssert.assertEquals() for JSON testing

    master

    JSONassert allows you to compare a JSON string against a JSONObject by evaluating their logical structure rather than a literal string comparison. This is ideal for testing REST interfaces where field order might change.

    Use the following method signature: JSONAssert.assertEquals(expectedJSONString, actualJSON, strictMode)

    • expectedJSONString: A String representing the expected JSON structure.
    • actualJSON: The actual JSONObject (or JSON string) received from your application.
    • strictMode: A boolean flag. When set to false (recommended), the comparison is non-strict, meaning it forgives reordering of data and allows extra fields in the actual JSON as long as the expected elements are present.
    JSONObject data = getRESTData("/friends/367.json");
    String expected = "{friends:[{id:123,name:\"Corby Page\"},{id:456,name:\"Carter Page\"}]}";
    JSONAssert.assertEquals(expected, data, false);