Class JsonAssert

java.lang.Object
cloud.opencode.base.test.assertion.JsonAssert

public final class JsonAssert extends Object
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 Details

    • assertThat

      public static JsonAssert assertThat(String actual)
      Creates assertion for JSON string. 为JSON字符串创建断言。
      Parameters:
      actual - the actual JSON string | 实际JSON字符串
      Returns:
      the assertion | 断言
    • isNull

      public JsonAssert isNull()
      Asserts that JSON is null. 断言JSON为null。
      Returns:
      this | 此对象
    • isNotNull

      public JsonAssert isNotNull()
      Asserts that JSON is not null. 断言JSON不为null。
      Returns:
      this | 此对象
    • isValidJson

      public JsonAssert isValidJson()
      Asserts that string is valid JSON. 断言字符串是有效的JSON。
      Returns:
      this | 此对象
    • isJsonObject

      public JsonAssert isJsonObject()
      Asserts that JSON is an object. 断言JSON是对象。
      Returns:
      this | 此对象
    • isJsonArray

      public JsonAssert isJsonArray()
      Asserts that JSON is an array. 断言JSON是数组。
      Returns:
      this | 此对象
    • containsKey

      public JsonAssert containsKey(String key)
      Asserts that JSON contains key. 断言JSON包含键。
      Parameters:
      key - the key | 键
      Returns:
      this | 此对象
    • doesNotContainKey

      public JsonAssert doesNotContainKey(String key)
      Asserts that JSON does not contain key. 断言JSON不包含键。
      Parameters:
      key - the key | 键
      Returns:
      this | 此对象
    • containsValue

      public JsonAssert containsValue(String value)
      Asserts that JSON contains string value. 断言JSON包含字符串值。
      Parameters:
      value - the value | 值
      Returns:
      this | 此对象
    • hasKeyValue

      public JsonAssert hasKeyValue(String key, String value)
      Asserts that JSON has key-value pair. 断言JSON有键值对。
      Parameters:
      key - the key | 键
      value - the string value | 字符串值
      Returns:
      this | 此对象
    • hasKeyValue

      public JsonAssert hasKeyValue(String key, Number value)
      Asserts that JSON has key-value pair with number. 断言JSON有数值键值对。
      Parameters:
      key - the key | 键
      value - the numeric value | 数值
      Returns:
      this | 此对象
    • hasKeyValue

      public JsonAssert hasKeyValue(String key, boolean value)
      Asserts that JSON has key-value pair with boolean. 断言JSON有布尔键值对。
      Parameters:
      key - the key | 键
      value - the boolean value | 布尔值
      Returns:
      this | 此对象
    • hasNullValue

      public JsonAssert hasNullValue(String key)
      Asserts that JSON has null value for key. 断言JSON的键值为null。
      Parameters:
      key - the key | 键
      Returns:
      this | 此对象
    • isEqualTo

      public JsonAssert isEqualTo(String expected)
      Asserts that JSON equals another JSON (ignoring whitespace). 断言JSON等于另一个JSON(忽略空白)。
      Parameters:
      expected - the expected JSON | 期望JSON
      Returns:
      this | 此对象
    • hasLength

      public JsonAssert hasLength(int length)
      Asserts that JSON has specified length. 断言JSON有指定长度。
      Parameters:
      length - the expected length | 期望长度
      Returns:
      this | 此对象
    • isEmptyObject

      public JsonAssert isEmptyObject()
      Asserts that JSON is empty object. 断言JSON是空对象。
      Returns:
      this | 此对象
    • isEmptyArray

      public JsonAssert isEmptyArray()
      Asserts that JSON is empty array. 断言JSON是空数组。
      Returns:
      this | 此对象