Class JsonAssert
java.lang.Object
cloud.opencode.base.test.assertion.JsonAssert
JSON Assert - Fluent assertions for JSON strings
JSON断言 - JSON字符串的流式断言
Provides comprehensive assertion methods for JSON string testing.
为JSON字符串测试提供全面的断言方法。
Features | 主要功能:
- JSON structure validation (object, array) - JSON结构验证(对象、数组)
- Key and value containment checks - 键和值包含检查
- Key-value pair assertions with multiple types - 多类型键值对断言
- Whitespace-normalized equality comparison - 空白规范化相等比较
Usage Examples | 使用示例:
JsonAssert.assertThat(jsonString)
.isValidJson()
.containsKey("name")
.hasValue("$.name", "John")
.hasArraySize("$.items", 3);
Security | 安全性:
- Thread-safe: No (not designed for concurrent use) - 线程安全: 否(非设计用于并发使用)
- Null-safe: Yes (validates non-null JSON) - 空值安全: 是(验证非空JSON)
- Since:
- JDK 25, opencode-base-test V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic JsonAssertassertThat(String actual) Creates assertion for JSON string.containsKey(String key) Asserts that JSON contains key.containsValue(String value) Asserts that JSON contains string value.doesNotContainKey(String key) Asserts that JSON does not contain key.hasKeyValue(String key, boolean value) Asserts that JSON has key-value pair with boolean.hasKeyValue(String key, Number value) Asserts that JSON has key-value pair with number.hasKeyValue(String key, String value) Asserts that JSON has key-value pair.hasLength(int length) Asserts that JSON has specified length.hasNullValue(String key) Asserts that JSON has null value for key.Asserts that JSON is empty array.Asserts that JSON is empty object.Asserts that JSON equals another JSON (ignoring whitespace).Asserts that JSON is an array.Asserts that JSON is an object.Asserts that JSON is not null.isNull()Asserts that JSON is null.Asserts that string is valid JSON.
-
Method Details
-
assertThat
Creates assertion for JSON string. 为JSON字符串创建断言。- Parameters:
actual- the actual JSON string | 实际JSON字符串- Returns:
- the assertion | 断言
-
isNull
-
isNotNull
-
isValidJson
Asserts that string is valid JSON. 断言字符串是有效的JSON。- Returns:
- this | 此对象
-
isJsonObject
-
isJsonArray
-
containsKey
Asserts that JSON contains key. 断言JSON包含键。- Parameters:
key- the key | 键- Returns:
- this | 此对象
-
doesNotContainKey
Asserts that JSON does not contain key. 断言JSON不包含键。- Parameters:
key- the key | 键- Returns:
- this | 此对象
-
containsValue
Asserts that JSON contains string value. 断言JSON包含字符串值。- Parameters:
value- the value | 值- Returns:
- this | 此对象
-
hasKeyValue
Asserts that JSON has key-value pair. 断言JSON有键值对。- Parameters:
key- the key | 键value- the string value | 字符串值- Returns:
- this | 此对象
-
hasKeyValue
Asserts that JSON has key-value pair with number. 断言JSON有数值键值对。- Parameters:
key- the key | 键value- the numeric value | 数值- Returns:
- this | 此对象
-
hasKeyValue
Asserts that JSON has key-value pair with boolean. 断言JSON有布尔键值对。- Parameters:
key- the key | 键value- the boolean value | 布尔值- Returns:
- this | 此对象
-
hasNullValue
Asserts that JSON has null value for key. 断言JSON的键值为null。- Parameters:
key- the key | 键- Returns:
- this | 此对象
-
isEqualTo
Asserts that JSON equals another JSON (ignoring whitespace). 断言JSON等于另一个JSON(忽略空白)。- Parameters:
expected- the expected JSON | 期望JSON- Returns:
- this | 此对象
-
hasLength
Asserts that JSON has specified length. 断言JSON有指定长度。- Parameters:
length- the expected length | 期望长度- Returns:
- this | 此对象
-
isEmptyObject
Asserts that JSON is empty object. 断言JSON是空对象。- Returns:
- this | 此对象
-
isEmptyArray
-