Class SoftAssert

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

public final class SoftAssert extends Object
Soft Assert - Collects assertion failures without throwing immediately 软断言 - 收集断言失败而不立即抛出

A soft assertion class that accumulates assertion failures during test execution and throws all collected errors at once when assertAll() is called.

软断言类,在测试执行期间累积断言失败,并在调用 assertAll() 时一次性抛出所有收集的错误。

Features | 主要功能:

  • Collect multiple assertion failures without stopping test execution - 收集多个断言失败而不停止测试执行
  • Fluent API for chained assertions - 流式API用于链式断言
  • Thread-safe using CopyOnWriteArrayList - 使用CopyOnWriteArrayList实现线程安全
  • Detailed failure reporting with all collected errors - 详细的失败报告,包含所有收集的错误

Usage Examples | 使用示例:

SoftAssert softAssert = new SoftAssert();
softAssert.isNotNull(value1)
          .isEqualTo(expected, actual)
          .isTrue(condition);
softAssert.assertAll(); // Throws if any assertion failed

Security | 安全性:

  • Thread-safe: Yes (CopyOnWriteArrayList for failures) - 线程安全: 是(失败列表使用CopyOnWriteArrayList)
  • Null-safe: Yes (handles null values gracefully) - 空值安全: 是(优雅处理空值)
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • SoftAssert

      public SoftAssert()
      Creates a new SoftAssert instance 创建新的SoftAssert实例
  • Method Details

    • isNull

      public SoftAssert isNull(Object actual)
      Asserts that the object is null 断言对象为null
      Parameters:
      actual - the actual value | 实际值
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isNull

      public SoftAssert isNull(Object actual, String message)
      Asserts that the object is null with custom message 断言对象为null(带自定义消息)
      Parameters:
      actual - the actual value | 实际值
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isNull

      public SoftAssert isNull(Object actual, Supplier<String> messageSupplier)
      Asserts that the object is null with lazy message 断言对象为null(带延迟消息)
      Parameters:
      actual - the actual value | 实际值
      messageSupplier - the message supplier | 消息提供者
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isNotNull

      public SoftAssert isNotNull(Object actual)
      Asserts that the object is not null 断言对象不为null
      Parameters:
      actual - the actual value | 实际值
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isNotNull

      public SoftAssert isNotNull(Object actual, String message)
      Asserts that the object is not null with custom message 断言对象不为null(带自定义消息)
      Parameters:
      actual - the actual value | 实际值
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isNotNull

      public SoftAssert isNotNull(Object actual, Supplier<String> messageSupplier)
      Asserts that the object is not null with lazy message 断言对象不为null(带延迟消息)
      Parameters:
      actual - the actual value | 实际值
      messageSupplier - the message supplier | 消息提供者
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isEqualTo

      public SoftAssert isEqualTo(Object expected, Object actual)
      Asserts that two objects are equal 断言两个对象相等
      Parameters:
      expected - the expected value | 期望值
      actual - the actual value | 实际值
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isEqualTo

      public SoftAssert isEqualTo(Object expected, Object actual, String message)
      Asserts that two objects are equal with custom message 断言两个对象相等(带自定义消息)
      Parameters:
      expected - the expected value | 期望值
      actual - the actual value | 实际值
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isEqualTo

      public SoftAssert isEqualTo(Object expected, Object actual, Supplier<String> messageSupplier)
      Asserts that two objects are equal with lazy message 断言两个对象相等(带延迟消息)
      Parameters:
      expected - the expected value | 期望值
      actual - the actual value | 实际值
      messageSupplier - the message supplier | 消息提供者
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isNotEqualTo

      public SoftAssert isNotEqualTo(Object unexpected, Object actual)
      Asserts that two objects are not equal 断言两个对象不相等
      Parameters:
      unexpected - the unexpected value | 不期望值
      actual - the actual value | 实际值
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isNotEqualTo

      public SoftAssert isNotEqualTo(Object unexpected, Object actual, String message)
      Asserts that two objects are not equal with custom message 断言两个对象不相等(带自定义消息)
      Parameters:
      unexpected - the unexpected value | 不期望值
      actual - the actual value | 实际值
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isNotEqualTo

      public SoftAssert isNotEqualTo(Object unexpected, Object actual, Supplier<String> messageSupplier)
      Asserts that two objects are not equal with lazy message 断言两个对象不相等(带延迟消息)
      Parameters:
      unexpected - the unexpected value | 不期望值
      actual - the actual value | 实际值
      messageSupplier - the message supplier | 消息提供者
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isTrue

      public SoftAssert isTrue(boolean condition)
      Asserts that the condition is true 断言条件为真
      Parameters:
      condition - the condition | 条件
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isTrue

      public SoftAssert isTrue(boolean condition, String message)
      Asserts that the condition is true with custom message 断言条件为真(带自定义消息)
      Parameters:
      condition - the condition | 条件
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isTrue

      public SoftAssert isTrue(boolean condition, Supplier<String> messageSupplier)
      Asserts that the condition is true with lazy message 断言条件为真(带延迟消息)
      Parameters:
      condition - the condition | 条件
      messageSupplier - the message supplier | 消息提供者
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isFalse

      public SoftAssert isFalse(boolean condition)
      Asserts that the condition is false 断言条件为假
      Parameters:
      condition - the condition | 条件
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isFalse

      public SoftAssert isFalse(boolean condition, String message)
      Asserts that the condition is false with custom message 断言条件为假(带自定义消息)
      Parameters:
      condition - the condition | 条件
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isFalse

      public SoftAssert isFalse(boolean condition, Supplier<String> messageSupplier)
      Asserts that the condition is false with lazy message 断言条件为假(带延迟消息)
      Parameters:
      condition - the condition | 条件
      messageSupplier - the message supplier | 消息提供者
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isEmpty

      public SoftAssert isEmpty(String actual)
      Asserts that the string is empty 断言字符串为空
      Parameters:
      actual - the actual string | 实际字符串
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isEmpty

      public SoftAssert isEmpty(String actual, String message)
      Asserts that the string is empty with custom message 断言字符串为空(带自定义消息)
      Parameters:
      actual - the actual string | 实际字符串
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isNotEmpty

      public SoftAssert isNotEmpty(String actual)
      Asserts that the string is not empty 断言字符串不为空
      Parameters:
      actual - the actual string | 实际字符串
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isNotEmpty

      public SoftAssert isNotEmpty(String actual, String message)
      Asserts that the string is not empty with custom message 断言字符串不为空(带自定义消息)
      Parameters:
      actual - the actual string | 实际字符串
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • contains

      public SoftAssert contains(String actual, String substring)
      Asserts that the string contains the substring 断言字符串包含子串
      Parameters:
      actual - the actual string | 实际字符串
      substring - the expected substring | 期望子串
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • contains

      public SoftAssert contains(String actual, String substring, String message)
      Asserts that the string contains the substring with custom message 断言字符串包含子串(带自定义消息)
      Parameters:
      actual - the actual string | 实际字符串
      substring - the expected substring | 期望子串
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • startsWith

      public SoftAssert startsWith(String actual, String prefix)
      Asserts that the string starts with the prefix 断言字符串以前缀开始
      Parameters:
      actual - the actual string | 实际字符串
      prefix - the expected prefix | 期望前缀
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • startsWith

      public SoftAssert startsWith(String actual, String prefix, String message)
      Asserts that the string starts with the prefix with custom message 断言字符串以前缀开始(带自定义消息)
      Parameters:
      actual - the actual string | 实际字符串
      prefix - the expected prefix | 期望前缀
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • endsWith

      public SoftAssert endsWith(String actual, String suffix)
      Asserts that the string ends with the suffix 断言字符串以后缀结束
      Parameters:
      actual - the actual string | 实际字符串
      suffix - the expected suffix | 期望后缀
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • endsWith

      public SoftAssert endsWith(String actual, String suffix, String message)
      Asserts that the string ends with the suffix with custom message 断言字符串以后缀结束(带自定义消息)
      Parameters:
      actual - the actual string | 实际字符串
      suffix - the expected suffix | 期望后缀
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isGreaterThan

      public SoftAssert isGreaterThan(Number actual, Number expected)
      Asserts that the actual number is greater than the expected 断言实际数值大于期望值
      Parameters:
      actual - the actual number | 实际数值
      expected - the expected number | 期望数值
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isGreaterThan

      public SoftAssert isGreaterThan(Number actual, Number expected, String message)
      Asserts that the actual number is greater than the expected with custom message 断言实际数值大于期望值(带自定义消息)
      Parameters:
      actual - the actual number | 实际数值
      expected - the expected number | 期望数值
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isGreaterThanOrEqualTo

      public SoftAssert isGreaterThanOrEqualTo(Number actual, Number expected)
      Asserts that the actual number is greater than or equal to the expected 断言实际数值大于或等于期望值
      Parameters:
      actual - the actual number | 实际数值
      expected - the expected number | 期望数值
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isGreaterThanOrEqualTo

      public SoftAssert isGreaterThanOrEqualTo(Number actual, Number expected, String message)
      Asserts that the actual number is greater than or equal to the expected with custom message 断言实际数值大于或等于期望值(带自定义消息)
      Parameters:
      actual - the actual number | 实际数值
      expected - the expected number | 期望数值
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isLessThan

      public SoftAssert isLessThan(Number actual, Number expected)
      Asserts that the actual number is less than the expected 断言实际数值小于期望值
      Parameters:
      actual - the actual number | 实际数值
      expected - the expected number | 期望数值
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isLessThan

      public SoftAssert isLessThan(Number actual, Number expected, String message)
      Asserts that the actual number is less than the expected with custom message 断言实际数值小于期望值(带自定义消息)
      Parameters:
      actual - the actual number | 实际数值
      expected - the expected number | 期望数值
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isLessThanOrEqualTo

      public SoftAssert isLessThanOrEqualTo(Number actual, Number expected)
      Asserts that the actual number is less than or equal to the expected 断言实际数值小于或等于期望值
      Parameters:
      actual - the actual number | 实际数值
      expected - the expected number | 期望数值
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isLessThanOrEqualTo

      public SoftAssert isLessThanOrEqualTo(Number actual, Number expected, String message)
      Asserts that the actual number is less than or equal to the expected with custom message 断言实际数值小于或等于期望值(带自定义消息)
      Parameters:
      actual - the actual number | 实际数值
      expected - the expected number | 期望数值
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isBetween

      public SoftAssert isBetween(Number actual, Number min, Number max)
      Asserts that the actual number is between min and max (inclusive) 断言实际数值在最小值和最大值之间(包含)
      Parameters:
      actual - the actual number | 实际数值
      min - the minimum value | 最小值
      max - the maximum value | 最大值
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • isBetween

      public SoftAssert isBetween(Number actual, Number min, Number max, String message)
      Asserts that the actual number is between min and max (inclusive) with custom message 断言实际数值在最小值和最大值之间(包含)(带自定义消息)
      Parameters:
      actual - the actual number | 实际数值
      min - the minimum value | 最小值
      max - the maximum value | 最大值
      message - the custom message | 自定义消息
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • assertAll

      public void assertAll()
      Asserts all collected assertions, throwing if any failed 断言所有收集的断言,如果有任何失败则抛出

      Throws an AssertionError if any assertions have failed, with a message containing all failure details.

      如果有任何断言失败,则抛出AssertionError,消息中包含所有失败详情。

      Throws:
      AssertionError - if any assertions have failed | 如果有任何断言失败
    • assertAll

      public void assertAll(String heading)
      Asserts all collected assertions with a custom header message 断言所有收集的断言(带自定义头消息)
      Parameters:
      heading - the heading for the failure message | 失败消息的标题
      Throws:
      AssertionError - if any assertions have failed | 如果有任何断言失败
    • hasFailures

      public boolean hasFailures()
      Checks if there are any collected failures 检查是否有任何收集的失败
      Returns:
      true if there are failures | 如果有失败返回true
    • getFailureCount

      public int getFailureCount()
      Gets the count of collected failures 获取收集的失败数量
      Returns:
      the number of failures | 失败数量
    • getFailures

      public List<AssertionError> getFailures()
      Gets an unmodifiable list of collected failures 获取收集的失败的不可修改列表
      Returns:
      unmodifiable list of assertion errors | 不可修改的断言错误列表
    • reset

      public SoftAssert reset()
      Resets the soft assert by clearing all collected failures 通过清除所有收集的失败来重置软断言
      Returns:
      this instance for chaining | 返回实例用于链式调用
    • toString

      public String toString()
      Returns a string representation of this SoftAssert 返回此SoftAssert的字符串表示
      Overrides:
      toString in class Object
      Returns:
      string representation | 字符串表示