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:
[1,2,{a:"b"}]
or [{pet:"cat"},{car:"Ford"}]
)[[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.
Modifier and Type | Method and Description |
---|---|
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,
JSONCompareMode compareMode)
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.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 expectedStr,
org.json.JSONObject actual,
JSONCompareMode compareMode)
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.
|
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 |
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,
JSONCompareMode compareMode)
Asserts that the JSONObject provided does not match the expected JSONObject.
|
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 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 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,
boolean strict)
Asserts that the JSONArray 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.
|
public static void assertEquals(String expectedStr, org.json.JSONObject actual, boolean strict) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactual
- JSONObject to comparestrict
- Enables strict checkingorg.json.JSONException
public static void assertNotEquals(String expectedStr, org.json.JSONObject actual, boolean strict) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactual
- JSONObject to comparestrict
- Enables strict checkingorg.json.JSONException
#assertEquals(String JSONObject, boolean)
public static void assertEquals(String expectedStr, org.json.JSONObject actual, JSONCompareMode compareMode) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to useorg.json.JSONException
public static void assertNotEquals(String expectedStr, org.json.JSONObject actual, JSONCompareMode compareMode) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to useorg.json.JSONException
assertEquals(String, JSONObject, JSONCompareMode)
public static void assertEquals(String expectedStr, org.json.JSONArray actual, boolean strict) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactual
- JSONArray to comparestrict
- Enables strict checkingorg.json.JSONException
public static void assertNotEquals(String expectedStr, org.json.JSONArray actual, boolean strict) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactual
- JSONArray to comparestrict
- Enables strict checkingorg.json.JSONException
public static void assertEquals(String expectedStr, org.json.JSONArray actual, JSONCompareMode compareMode) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to useorg.json.JSONException
public static void assertNotEquals(String expectedStr, org.json.JSONArray actual, JSONCompareMode compareMode) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to useorg.json.JSONException
public static void assertEquals(String expectedStr, String actualStr, boolean strict) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactualStr
- String to comparestrict
- Enables strict checkingorg.json.JSONException
public static void assertNotEquals(String expectedStr, String actualStr, boolean strict) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactualStr
- String to comparestrict
- Enables strict checkingorg.json.JSONException
public static void assertEquals(String expectedStr, String actualStr, JSONCompareMode compareMode) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactualStr
- String to comparecompareMode
- Specifies which comparison mode to useorg.json.JSONException
public static void assertNotEquals(String expectedStr, String actualStr, JSONCompareMode compareMode) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactualStr
- String to comparecompareMode
- Specifies which comparison mode to useorg.json.JSONException
public static void assertEquals(String expectedStr, String actualStr, JSONComparator comparator) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactualStr
- String to comparecomparator
- Comparatororg.json.JSONException
public static void assertNotEquals(String expectedStr, String actualStr, JSONComparator comparator) throws org.json.JSONException
AssertionError
.expectedStr
- Expected JSON stringactualStr
- String to comparecomparator
- Comparatororg.json.JSONException
public static void assertEquals(org.json.JSONObject expected, org.json.JSONObject actual, boolean strict) throws org.json.JSONException
AssertionError
.expected
- Expected JSONObjectactual
- JSONObject to comparestrict
- Enables strict checkingorg.json.JSONException
public static void assertNotEquals(org.json.JSONObject expected, org.json.JSONObject actual, boolean strict) throws org.json.JSONException
AssertionError
.expected
- Expected JSONObjectactual
- JSONObject to comparestrict
- Enables strict checkingorg.json.JSONException
public static void assertEquals(org.json.JSONObject expected, org.json.JSONObject actual, JSONCompareMode compareMode) throws org.json.JSONException
AssertionError
.expected
- Expected JSONObjectactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to useorg.json.JSONException
public static void assertNotEquals(org.json.JSONObject expected, org.json.JSONObject actual, JSONCompareMode compareMode) throws org.json.JSONException
AssertionError
.expected
- Expected JSONObjectactual
- JSONObject to comparecompareMode
- Specifies which comparison mode to useorg.json.JSONException
public static void assertEquals(org.json.JSONArray expected, org.json.JSONArray actual, boolean strict) throws org.json.JSONException
AssertionError
.expected
- Expected JSONArrayactual
- JSONArray to comparestrict
- Enables strict checkingorg.json.JSONException
public static void assertNotEquals(org.json.JSONArray expected, org.json.JSONArray actual, boolean strict) throws org.json.JSONException
AssertionError
.expected
- Expected JSONArrayactual
- JSONArray to comparestrict
- Enables strict checkingorg.json.JSONException
public static void assertEquals(org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareMode compareMode) throws org.json.JSONException
AssertionError
.expected
- Expected JSONArrayactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to useorg.json.JSONException
public static void assertNotEquals(org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareMode compareMode) throws org.json.JSONException
AssertionError
.expected
- Expected JSONArrayactual
- JSONArray to comparecompareMode
- Specifies which comparison mode to useorg.json.JSONException
Copyright © 2016. All rights reserved.