Class JSONAssert
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:
- Unless the order is strict, checking does not handle mixed types in the JSONArray
(e.g.
[1,2,{a:"b"}]
or[{pet:"cat"},{car:"Ford"}]
) - Unless the order is strict, checking cannot handle arrays of arrays (e.g.
[[1,2],[3,4]]
)
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
Modifier and TypeMethodDescriptionstatic void
assertEquals
(String expectedStr, String actualStr, boolean strict) Asserts that the JSONArray provided matches the expected string.static void
assertEquals
(String message, String expectedStr, String actualStr, boolean strict) Asserts that the JSONArray provided matches the expected string.static void
assertEquals
(String message, String expectedStr, String actualStr, JSONComparator comparator) Asserts that the json string provided matches the expected string.static void
assertEquals
(String message, String expectedStr, String actualStr, JSONCompareMode compareMode) Asserts that the JSONArray provided matches the expected string.static void
assertEquals
(String message, String expectedStr, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided matches the expected string.static void
assertEquals
(String message, String expectedStr, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided matches the expected string.static void
assertEquals
(String message, String expectedStr, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided matches the expected string.static void
assertEquals
(String message, String expectedStr, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided matches the expected string.static void
assertEquals
(String expectedStr, String actualStr, JSONComparator comparator) Asserts that the json string provided matches the expected string.static void
assertEquals
(String expectedStr, String actualStr, JSONCompareMode compareMode) Asserts that the JSONArray provided matches the expected string.static void
assertEquals
(String expectedStr, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided matches the expected string.static void
assertEquals
(String message, org.json.JSONArray expected, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided matches the expected JSONArray.static void
assertEquals
(String message, org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided matches the expected JSONArray.static void
assertEquals
(String expectedStr, org.json.JSONArray actual, JSONCompareMode compareMode) 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 message, org.json.JSONObject expected, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided matches the expected JSONObject.static void
assertEquals
(String message, org.json.JSONObject expected, org.json.JSONObject actual, JSONComparator comparator) Asserts that the JSONObject provided matches the expected JSONObject.static void
assertEquals
(String message, org.json.JSONObject expected, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided matches the expected JSONObject.static void
assertEquals
(String expectedStr, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided matches the expected string.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.JSONArray expected, org.json.JSONArray actual, JSONCompareMode compareMode) 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
(org.json.JSONObject expected, org.json.JSONObject actual, JSONComparator comparator) Asserts that the JSONObject provided matches the expected JSONObject.static void
assertEquals
(org.json.JSONObject expected, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided matches the expected JSONObject.static void
assertNotEquals
(String expectedStr, String actualStr, boolean strict) Asserts that the JSONArray provided does not match the expected string.static void
assertNotEquals
(String message, String expectedStr, String actualStr, boolean strict) Asserts that the JSONArray provided does not match the expected string.static void
assertNotEquals
(String message, String expectedStr, String actualStr, JSONComparator comparator) Asserts that the json string provided does not match the expected string.static void
assertNotEquals
(String message, String expectedStr, String actualStr, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected string.static void
assertNotEquals
(String message, String expectedStr, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided does not match the expected string.static void
assertNotEquals
(String message, String expectedStr, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected string.static void
assertNotEquals
(String message, String expectedStr, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided does not match the expected string.static void
assertNotEquals
(String message, String expectedStr, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided does not match the expected string.static void
assertNotEquals
(String expectedStr, String actualStr, JSONComparator comparator) Asserts that the json string provided does not match the expected string.static void
assertNotEquals
(String expectedStr, String actualStr, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected string.static void
assertNotEquals
(String expectedStr, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided does not match the expected string.static void
assertNotEquals
(String message, org.json.JSONArray expected, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided does not match the expected JSONArray.static void
assertNotEquals
(String message, org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected JSONArray.static void
assertNotEquals
(String expectedStr, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected string.static void
assertNotEquals
(String expectedStr, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided does not match the expected string.static void
assertNotEquals
(String message, org.json.JSONObject expected, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided does not match the expected JSONObject.static void
assertNotEquals
(String message, org.json.JSONObject expected, org.json.JSONObject actual, JSONComparator comparator) Asserts that the JSONObject provided does not match the expected JSONObject.static void
assertNotEquals
(String message, org.json.JSONObject expected, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided does not match the expected JSONObject.static void
assertNotEquals
(String expectedStr, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided does not match the expected string.static void
assertNotEquals
(org.json.JSONArray expected, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided does not match the expected JSONArray.static void
assertNotEquals
(org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected JSONArray.static void
assertNotEquals
(org.json.JSONObject expected, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided does not match the expected JSONObject.static void
assertNotEquals
(org.json.JSONObject expected, org.json.JSONObject actual, JSONComparator comparator) Asserts that the JSONObject provided does not match the expected JSONObject.static void
assertNotEquals
(org.json.JSONObject expected, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided does not match the expected JSONObject.
-
Method Details
-
assertEquals
Asserts that the JSONObject provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactual
- JSONObject to comparestrict
- Enables strict checking
-
assertEquals
public static void assertEquals(String message, String expectedStr, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactual
- JSONObject to comparestrict
- Enables strict checking
-
assertNotEquals
Asserts that the JSONObject provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactual
- JSONObject to comparestrict
- Enables strict checking- See Also:
-
assertNotEquals
public static void assertNotEquals(String message, String expectedStr, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactual
- JSONObject to comparestrict
- Enables strict checking- See Also:
-
assertEquals
public static void assertEquals(String expectedStr, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to use
-
assertEquals
public static void assertEquals(String message, String expectedStr, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to use
-
assertNotEquals
public static void assertNotEquals(String expectedStr, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to use- See Also:
-
assertNotEquals
public static void assertNotEquals(String message, String expectedStr, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to use- See Also:
-
assertEquals
Asserts that the JSONArray provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactual
- JSONArray to comparestrict
- Enables strict checking
-
assertEquals
public static void assertEquals(String message, String expectedStr, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactual
- JSONArray to comparestrict
- Enables strict checking
-
assertNotEquals
Asserts that the JSONArray provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactual
- JSONArray to comparestrict
- Enables strict checking
-
assertNotEquals
public static void assertNotEquals(String message, String expectedStr, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactual
- JSONArray to comparestrict
- Enables strict checking
-
assertEquals
public static void assertEquals(String expectedStr, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to use
-
assertEquals
public static void assertEquals(String message, String expectedStr, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to use
-
assertNotEquals
public static void assertNotEquals(String expectedStr, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to use
-
assertNotEquals
public static void assertNotEquals(String message, String expectedStr, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to use
-
assertEquals
Asserts that the JSONArray provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactualStr
- String to comparestrict
- Enables strict checking
-
assertEquals
public static void assertEquals(String message, String expectedStr, String actualStr, boolean strict) Asserts that the JSONArray provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactualStr
- String to comparestrict
- Enables strict checking
-
assertNotEquals
Asserts that the JSONArray provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactualStr
- String to comparestrict
- Enables strict checking
-
assertNotEquals
public static void assertNotEquals(String message, String expectedStr, String actualStr, boolean strict) Asserts that the JSONArray provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactualStr
- String to comparestrict
- Enables strict checking
-
assertEquals
Asserts that the JSONArray provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactualStr
- String to comparecompareMode
- Specifies which comparison mode to use
-
assertEquals
public static void assertEquals(String message, String expectedStr, String actualStr, JSONCompareMode compareMode) Asserts that the JSONArray provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactualStr
- String to comparecompareMode
- Specifies which comparison mode to use
-
assertNotEquals
public static void assertNotEquals(String expectedStr, String actualStr, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactualStr
- String to comparecompareMode
- Specifies which comparison mode to use
-
assertNotEquals
public static void assertNotEquals(String message, String expectedStr, String actualStr, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactualStr
- String to comparecompareMode
- Specifies which comparison mode to use
-
assertEquals
Asserts that the json string provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactualStr
- String to comparecomparator
- Comparator
-
assertEquals
public static void assertEquals(String message, String expectedStr, String actualStr, JSONComparator comparator) Asserts that the json string provided matches the expected string. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactualStr
- String to comparecomparator
- Comparator
-
assertNotEquals
Asserts that the json string provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
expectedStr
- Expected JSON stringactualStr
- String to comparecomparator
- Comparator
-
assertNotEquals
public static void assertNotEquals(String message, String expectedStr, String actualStr, JSONComparator comparator) Asserts that the json string provided does not match the expected string. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpectedStr
- Expected JSON stringactualStr
- String to comparecomparator
- Comparator
-
assertEquals
public static void assertEquals(org.json.JSONObject expected, org.json.JSONObject actual, JSONComparator comparator) Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws anAssertionError
.- Parameters:
expected
- Expected JSONObjectactual
- JSONObject to comparecomparator
- Comparator
-
assertEquals
public static void assertEquals(String message, org.json.JSONObject expected, org.json.JSONObject actual, JSONComparator comparator) Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpected
- Expected JSONObjectactual
- JSONObject to comparecomparator
- Comparator
-
assertNotEquals
public static void assertNotEquals(org.json.JSONObject expected, org.json.JSONObject actual, JSONComparator comparator) Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws anAssertionError
.- Parameters:
expected
- Expected JSONObjectactual
- JSONObject to comparecomparator
- Comparator
-
assertNotEquals
public static void assertNotEquals(String message, org.json.JSONObject expected, org.json.JSONObject actual, JSONComparator comparator) Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpected
- Expected JSONObjectactual
- JSONObject to comparecomparator
- Comparator
-
assertEquals
public static void assertEquals(org.json.JSONObject expected, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws anAssertionError
.- Parameters:
expected
- Expected JSONObjectactual
- JSONObject to comparestrict
- Enables strict checking
-
assertEquals
public static void assertEquals(String message, org.json.JSONObject expected, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpected
- Expected JSONObjectactual
- JSONObject to comparestrict
- Enables strict checking
-
assertNotEquals
public static void assertNotEquals(org.json.JSONObject expected, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws anAssertionError
.- Parameters:
expected
- Expected JSONObjectactual
- JSONObject to comparestrict
- Enables strict checking
-
assertNotEquals
public static void assertNotEquals(String message, org.json.JSONObject expected, org.json.JSONObject actual, boolean strict) Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpected
- Expected JSONObjectactual
- JSONObject to comparestrict
- Enables strict checking
-
assertEquals
public static void assertEquals(org.json.JSONObject expected, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws anAssertionError
.- Parameters:
expected
- Expected JSONObjectactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to use
-
assertEquals
public static void assertEquals(String message, org.json.JSONObject expected, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpected
- Expected JSONObjectactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to use
-
assertNotEquals
public static void assertNotEquals(org.json.JSONObject expected, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws anAssertionError
.- Parameters:
expected
- Expected JSONObjectactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to use
-
assertNotEquals
public static void assertNotEquals(String message, org.json.JSONObject expected, org.json.JSONObject actual, JSONCompareMode compareMode) Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpected
- Expected JSONObjectactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to use
-
assertEquals
public static void assertEquals(org.json.JSONArray expected, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws anAssertionError
.- Parameters:
expected
- Expected JSONArrayactual
- JSONArray to comparestrict
- Enables strict checking
-
assertEquals
public static void assertEquals(String message, org.json.JSONArray expected, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpected
- Expected JSONArrayactual
- JSONArray to comparestrict
- Enables strict checking
-
assertNotEquals
public static void assertNotEquals(org.json.JSONArray expected, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided does not match the expected JSONArray. If it is it throws anAssertionError
.- Parameters:
expected
- Expected JSONArrayactual
- JSONArray to comparestrict
- Enables strict checking
-
assertNotEquals
public static void assertNotEquals(String message, org.json.JSONArray expected, org.json.JSONArray actual, boolean strict) Asserts that the JSONArray provided does not match the expected JSONArray. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpected
- Expected JSONArrayactual
- JSONArray to comparestrict
- Enables strict checking
-
assertEquals
public static void assertEquals(org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws anAssertionError
.- Parameters:
expected
- Expected JSONArrayactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to use
-
assertEquals
public static void assertEquals(String message, org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpected
- Expected JSONArrayactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to use
-
assertNotEquals
public static void assertNotEquals(org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected JSONArray. If it is it throws anAssertionError
.- Parameters:
expected
- Expected JSONArrayactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to use
-
assertNotEquals
public static void assertNotEquals(String message, org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareMode compareMode) Asserts that the JSONArray provided does not match the expected JSONArray. If it is it throws anAssertionError
.- Parameters:
message
- Error message to be displayed in case of assertion failureexpected
- Expected JSONArrayactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to use
-