org.skyscreamer.jsonassert
Class JSONAssert

java.lang.Object
  extended by org.skyscreamer.jsonassert.JSONAssert

public class JSONAssert
extends Object

A set of assertion methods useful for writing tests methods that return JSON.

There are two modes, strict and non-strict. In most cases, you will probably want to set strict to false, since that will make the tests less brittle.

Strict tests require all of the elements requested to be returned, and only those elements (ie, the tests are non-extensible). Arrays of elements must be returned in the same order as expected. For example, say I'm expecting:

{id:123,things['a','b','c']}

The following would match when doing non-strict checking, but would fail on strict checking:

{id:123,things['c','b','a'],anotherfield:'blah'}

This library uses org.json. It has fewer dependencies than other JSON libraries (like net.sf.json), making JSONassert more portable.

There are two known issues when dealing with non-strict comparisons:

You do not have to worry about encountering a false positive or false negative in these two edge cases. JSONassert will identify the conditions and throw a descriptive IllegalArgumentException. These cases will be fixed in future versions.


Method Summary
static void assertEquals(org.json.JSONArray expected, org.json.JSONArray actual, boolean strict)
          Asserts that the JSONArray provided matches the expected JSONArray.
static void assertEquals(org.json.JSONObject expected, org.json.JSONObject actual, boolean strict)
          Asserts that the JSONObject provided matches the expected JSONObject.
static void assertEquals(String expectedStr, org.json.JSONArray actual, boolean strict)
          Asserts that the JSONArray provided matches the expected string.
static void assertEquals(String expectedStr, org.json.JSONObject actual, boolean strict)
          Asserts that the JSONObject provided matches the expected string.
static void assertEquals(String expectedStr, String actualStr, boolean strict)
          Asserts that the JSONArray provided matches the expected string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

assertEquals

public static void assertEquals(String expectedStr,
                                org.json.JSONObject actual,
                                boolean strict)
                         throws org.json.JSONException
Asserts that the JSONObject provided matches the expected string. If it isn't it throws an AssertionError.

Parameters:
expectedStr - Expected JSON string
actual - JSONObject to compare
strict - Enables strict checking
Throws:
org.json.JSONException

assertEquals

public static void assertEquals(String expectedStr,
                                org.json.JSONArray actual,
                                boolean strict)
                         throws org.json.JSONException
Asserts that the JSONArray provided matches the expected string. If it isn't it throws an AssertionError.

Parameters:
expectedStr - Expected JSON string
actual - JSONArray to compare
strict - Enables strict checking
Throws:
org.json.JSONException

assertEquals

public static void assertEquals(String expectedStr,
                                String actualStr,
                                boolean strict)
                         throws org.json.JSONException
Asserts that the JSONArray provided matches the expected string. If it isn't it throws an AssertionError.

Parameters:
expectedStr - Expected JSON string
actualStr - String to compare
strict - Enables strict checking
Throws:
org.json.JSONException

assertEquals

public static void assertEquals(org.json.JSONObject expected,
                                org.json.JSONObject actual,
                                boolean strict)
                         throws org.json.JSONException
Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an AssertionError.

Parameters:
expected - Expected JSONObject
actual - JSONObject to compare
strict - Enables strict checking
Throws:
org.json.JSONException

assertEquals

public static void assertEquals(org.json.JSONArray expected,
                                org.json.JSONArray actual,
                                boolean strict)
                         throws org.json.JSONException
Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws an AssertionError.

Parameters:
expected - Expected JSONArray
actual - JSONArray to compare
strict - Enables strict checking
Throws:
org.json.JSONException


Copyright © 2012. All Rights Reserved.