Class SoftAssert
java.lang.Object
cloud.opencode.base.test.assertion.SoftAssert
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAsserts all collected assertions, throwing if any failed 断言所有收集的断言,如果有任何失败则抛出voidAsserts all collected assertions with a custom header message 断言所有收集的断言(带自定义头消息)Asserts that the string contains the substring 断言字符串包含子串Asserts that the string contains the substring with custom message 断言字符串包含子串(带自定义消息)Asserts that the string ends with the suffix 断言字符串以后缀结束Asserts that the string ends with the suffix with custom message 断言字符串以后缀结束(带自定义消息)intGets the count of collected failures 获取收集的失败数量Gets an unmodifiable list of collected failures 获取收集的失败的不可修改列表booleanChecks if there are any collected failures 检查是否有任何收集的失败Asserts that the actual number is between min and max (inclusive) 断言实际数值在最小值和最大值之间(包含)Asserts that the actual number is between min and max (inclusive) with custom message 断言实际数值在最小值和最大值之间(包含)(带自定义消息)Asserts that the string is empty 断言字符串为空Asserts that the string is empty with custom message 断言字符串为空(带自定义消息)Asserts that two objects are equal 断言两个对象相等Asserts that two objects are equal with custom message 断言两个对象相等(带自定义消息)Asserts that two objects are equal with lazy message 断言两个对象相等(带延迟消息)isFalse(boolean condition) Asserts that the condition is false 断言条件为假Asserts that the condition is false with custom message 断言条件为假(带自定义消息)Asserts that the condition is false with lazy message 断言条件为假(带延迟消息)isGreaterThan(Number actual, Number expected) Asserts that the actual number is greater than the expected 断言实际数值大于期望值isGreaterThan(Number actual, Number expected, String message) Asserts that the actual number is greater than the expected with custom message 断言实际数值大于期望值(带自定义消息)isGreaterThanOrEqualTo(Number actual, Number expected) Asserts that the actual number is greater than or equal to the expected 断言实际数值大于或等于期望值isGreaterThanOrEqualTo(Number actual, Number expected, String message) Asserts that the actual number is greater than or equal to the expected with custom message 断言实际数值大于或等于期望值(带自定义消息)isLessThan(Number actual, Number expected) Asserts that the actual number is less than the expected 断言实际数值小于期望值isLessThan(Number actual, Number expected, String message) Asserts that the actual number is less than the expected with custom message 断言实际数值小于期望值(带自定义消息)isLessThanOrEqualTo(Number actual, Number expected) Asserts that the actual number is less than or equal to the expected 断言实际数值小于或等于期望值isLessThanOrEqualTo(Number actual, Number expected, String message) Asserts that the actual number is less than or equal to the expected with custom message 断言实际数值小于或等于期望值(带自定义消息)isNotEmpty(String actual) Asserts that the string is not empty 断言字符串不为空isNotEmpty(String actual, String message) Asserts that the string is not empty with custom message 断言字符串不为空(带自定义消息)isNotEqualTo(Object unexpected, Object actual) Asserts that two objects are not equal 断言两个对象不相等isNotEqualTo(Object unexpected, Object actual, String message) Asserts that two objects are not equal with custom message 断言两个对象不相等(带自定义消息)isNotEqualTo(Object unexpected, Object actual, Supplier<String> messageSupplier) Asserts that two objects are not equal with lazy message 断言两个对象不相等(带延迟消息)Asserts that the object is not null 断言对象不为nullAsserts that the object is not null with custom message 断言对象不为null(带自定义消息)Asserts that the object is not null with lazy message 断言对象不为null(带延迟消息)Asserts that the object is null 断言对象为nullAsserts that the object is null with custom message 断言对象为null(带自定义消息)Asserts that the object is null with lazy message 断言对象为null(带延迟消息)isTrue(boolean condition) Asserts that the condition is true 断言条件为真Asserts that the condition is true with custom message 断言条件为真(带自定义消息)Asserts that the condition is true with lazy message 断言条件为真(带延迟消息)reset()Resets the soft assert by clearing all collected failures 通过清除所有收集的失败来重置软断言startsWith(String actual, String prefix) Asserts that the string starts with the prefix 断言字符串以前缀开始startsWith(String actual, String prefix, String message) Asserts that the string starts with the prefix with custom message 断言字符串以前缀开始(带自定义消息)toString()Returns a string representation of this SoftAssert 返回此SoftAssert的字符串表示
-
Constructor Details
-
SoftAssert
public SoftAssert()Creates a new SoftAssert instance 创建新的SoftAssert实例
-
-
Method Details
-
isNull
Asserts that the object is null 断言对象为null- Parameters:
actual- the actual value | 实际值- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isNull
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
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
Asserts that the object is not null 断言对象不为null- Parameters:
actual- the actual value | 实际值- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isNotNull
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
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
Asserts that two objects are equal 断言两个对象相等- Parameters:
expected- the expected value | 期望值actual- the actual value | 实际值- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isEqualTo
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
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
Asserts that two objects are not equal 断言两个对象不相等- Parameters:
unexpected- the unexpected value | 不期望值actual- the actual value | 实际值- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isNotEqualTo
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
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
Asserts that the condition is true 断言条件为真- Parameters:
condition- the condition | 条件- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isTrue
Asserts that the condition is true with custom message 断言条件为真(带自定义消息)- Parameters:
condition- the condition | 条件message- the custom message | 自定义消息- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isTrue
Asserts that the condition is true with lazy message 断言条件为真(带延迟消息)- Parameters:
condition- the condition | 条件messageSupplier- the message supplier | 消息提供者- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isFalse
Asserts that the condition is false 断言条件为假- Parameters:
condition- the condition | 条件- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isFalse
Asserts that the condition is false with custom message 断言条件为假(带自定义消息)- Parameters:
condition- the condition | 条件message- the custom message | 自定义消息- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isFalse
Asserts that the condition is false with lazy message 断言条件为假(带延迟消息)- Parameters:
condition- the condition | 条件messageSupplier- the message supplier | 消息提供者- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isEmpty
Asserts that the string is empty 断言字符串为空- Parameters:
actual- the actual string | 实际字符串- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isEmpty
Asserts that the string is empty with custom message 断言字符串为空(带自定义消息)- Parameters:
actual- the actual string | 实际字符串message- the custom message | 自定义消息- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isNotEmpty
Asserts that the string is not empty 断言字符串不为空- Parameters:
actual- the actual string | 实际字符串- Returns:
- this instance for chaining | 返回实例用于链式调用
-
isNotEmpty
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
Asserts that the string contains the substring 断言字符串包含子串- Parameters:
actual- the actual string | 实际字符串substring- the expected substring | 期望子串- Returns:
- this instance for chaining | 返回实例用于链式调用
-
contains
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
Asserts that the string starts with the prefix 断言字符串以前缀开始- Parameters:
actual- the actual string | 实际字符串prefix- the expected prefix | 期望前缀- Returns:
- this instance for chaining | 返回实例用于链式调用
-
startsWith
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
Asserts that the string ends with the suffix 断言字符串以后缀结束- Parameters:
actual- the actual string | 实际字符串suffix- the expected suffix | 期望后缀- Returns:
- this instance for chaining | 返回实例用于链式调用
-
endsWith
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
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
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
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
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
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
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
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
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
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
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
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
Gets an unmodifiable list of collected failures 获取收集的失败的不可修改列表- Returns:
- unmodifiable list of assertion errors | 不可修改的断言错误列表
-
reset
Resets the soft assert by clearing all collected failures 通过清除所有收集的失败来重置软断言- Returns:
- this instance for chaining | 返回实例用于链式调用
-
toString
-