Class OpenAssert
java.lang.Object
cloud.opencode.base.test.OpenAssert
Assertion Entry Class - Provides comprehensive assertion methods
断言入口类 - 提供全面的断言方法
Zero-dependency assertion library with support for basic, collection, string, numeric, and exception assertions.
零依赖断言库,支持基础、集合、字符串、数值和异常断言。
Features | 主要功能:
- Basic assertions (true, false, null, equals) - 基础断言
- Collection assertions (empty, size, contains) - 集合断言
- String assertions (blank, contains, matches) - 字符串断言
- Numeric assertions (greater, less, between) - 数值断言
- Exception assertions (throws, doesNotThrow) - 异常断言
- Timeout assertions - 超时断言
Usage Examples | 使用示例:
OpenAssert.assertTrue(condition);
OpenAssert.assertEquals(expected, actual);
OpenAssert.assertContains("hello", list);
OpenAssert.assertThrows(IllegalArgumentException.class, () -> { throw new IllegalArgumentException(); });
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: Yes (null checks where applicable) - 空值安全: 是(在适用处进行空值检查)
- Since:
- JDK 25, opencode-base-test V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceExecutable functional interface for exception assertions 用于异常断言的可执行函数式接口 -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Comparable<T>>
voidassertBetween(T actual, T min, T max) Asserts that actual is between min and max (inclusive) 断言实际值在最小值和最大值之间(包含)static voidassertBlank(String str) Asserts that string is blank 断言字符串为空白static voidassertContains(Object element, Collection<?> collection) Asserts that collection contains element 断言集合包含元素static voidassertContains(String expected, String actual) Asserts that string contains substring 断言字符串包含子串static voidassertContainsKey(Object key, Map<?, ?> map) Asserts that map contains key 断言映射包含键static voidassertDoesNotThrow(OpenAssert.Executable executable) Asserts that executable does not throw 断言可执行对象不抛出异常static voidassertEmpty(Collection<?> collection) Asserts that collection is empty 断言集合为空static voidassertEmpty(Map<?, ?> map) Asserts that map is empty 断言映射为空static voidassertEndsWith(String suffix, String actual) Asserts that string ends with suffix 断言字符串以后缀结束static voidassertEquals(double expected, double actual, double delta) Asserts that doubles are equal within delta 断言双精度数在误差范围内相等static voidassertEquals(Object expected, Object actual) Asserts that objects are equal 断言对象相等static voidassertEquals(Object expected, Object actual, String message) Asserts that objects are equal with message 断言对象相等(带消息)static voidassertFalse(boolean condition) Asserts that condition is false 断言条件为假static voidassertFalse(boolean condition, String message) Asserts that condition is false with message 断言条件为假(带消息)static <T extends Comparable<T>>
voidassertGreaterThan(T actual, T expected) Asserts that actual is greater than expected 断言实际值大于期望值static <T extends Comparable<T>>
voidassertLessThan(T actual, T expected) Asserts that actual is less than expected 断言实际值小于期望值static voidassertMatches(String regex, String actual) Asserts that string matches regex 断言字符串匹配正则表达式static voidassertNotBlank(String str) Asserts that string is not blank 断言字符串不为空白static voidassertNotEmpty(Collection<?> collection) Asserts that collection is not empty 断言集合不为空static voidassertNotEquals(Object unexpected, Object actual) Asserts that objects are not equal 断言对象不相等static voidassertNotNull(Object obj) Asserts that object is not null 断言对象不为nullstatic voidassertNotNull(Object obj, String message) Asserts that object is not null with message 断言对象不为null(带消息)static voidassertNotSame(Object unexpected, Object actual) Asserts that objects are not the same instance 断言对象不是相同实例static voidassertNull(Object obj) Asserts that object is null 断言对象为nullstatic voidassertNull(Object obj, String message) Asserts that object is null with message 断言对象为null(带消息)static voidassertSame(Object expected, Object actual) Asserts that objects are the same instance 断言对象是相同实例static voidassertSize(int expected, Collection<?> collection) Asserts collection size 断言集合大小static voidassertStartsWith(String prefix, String actual) Asserts that string starts with prefix 断言字符串以前缀开始static <T extends Throwable>
TassertThrows(Class<T> expectedType, OpenAssert.Executable executable) Asserts that executable throws expected exception 断言可执行对象抛出预期异常static voidassertTimeout(Duration timeout, OpenAssert.Executable executable) Asserts that executable completes within timeout 断言可执行对象在超时内完成static voidassertTrue(boolean condition) Asserts that condition is true 断言条件为真static voidassertTrue(boolean condition, String message) Asserts that condition is true with message 断言条件为真(带消息)static voidassertTrue(boolean condition, Supplier<String> messageSupplier) Asserts that condition is true with lazy message 断言条件为真(延迟消息)static voidfail()Fails unconditionally 无条件失败static voidFails unconditionally with message 无条件失败(带消息)
-
Method Details
-
assertTrue
public static void assertTrue(boolean condition) Asserts that condition is true 断言条件为真- Parameters:
condition- the condition | 条件
-
assertTrue
Asserts that condition is true with message 断言条件为真(带消息)- Parameters:
condition- the condition | 条件message- the message | 消息
-
assertTrue
-
assertFalse
public static void assertFalse(boolean condition) Asserts that condition is false 断言条件为假- Parameters:
condition- the condition | 条件
-
assertFalse
Asserts that condition is false with message 断言条件为假(带消息)- Parameters:
condition- the condition | 条件message- the message | 消息
-
assertNull
Asserts that object is null 断言对象为null- Parameters:
obj- the object | 对象
-
assertNull
-
assertNotNull
Asserts that object is not null 断言对象不为null- Parameters:
obj- the object | 对象
-
assertNotNull
-
assertEquals
-
assertEquals
-
assertNotEquals
-
assertSame
-
assertNotSame
-
assertEquals
public static void assertEquals(double expected, double actual, double delta) Asserts that doubles are equal within delta 断言双精度数在误差范围内相等- Parameters:
expected- the expected value | 期望值actual- the actual value | 实际值delta- the maximum delta | 最大误差
-
assertGreaterThan
Asserts that actual is greater than expected 断言实际值大于期望值- Type Parameters:
T- the comparable type | 可比较类型- Parameters:
actual- the actual value | 实际值expected- the expected value | 期望值
-
assertLessThan
Asserts that actual is less than expected 断言实际值小于期望值- Type Parameters:
T- the comparable type | 可比较类型- Parameters:
actual- the actual value | 实际值expected- the expected value | 期望值
-
assertBetween
Asserts that actual is between min and max (inclusive) 断言实际值在最小值和最大值之间(包含)- Type Parameters:
T- the comparable type | 可比较类型- Parameters:
actual- the actual value | 实际值min- the minimum value | 最小值max- the maximum value | 最大值
-
assertEmpty
Asserts that collection is empty 断言集合为空- Parameters:
collection- the collection | 集合
-
assertNotEmpty
Asserts that collection is not empty 断言集合不为空- Parameters:
collection- the collection | 集合
-
assertSize
Asserts collection size 断言集合大小- Parameters:
expected- the expected size | 期望大小collection- the collection | 集合
-
assertContains
Asserts that collection contains element 断言集合包含元素- Parameters:
element- the element | 元素collection- the collection | 集合
-
assertEmpty
Asserts that map is empty 断言映射为空- Parameters:
map- the map | 映射
-
assertContainsKey
-
assertBlank
Asserts that string is blank 断言字符串为空白- Parameters:
str- the string | 字符串
-
assertNotBlank
Asserts that string is not blank 断言字符串不为空白- Parameters:
str- the string | 字符串
-
assertContains
-
assertStartsWith
-
assertEndsWith
-
assertMatches
-
assertThrows
public static <T extends Throwable> T assertThrows(Class<T> expectedType, OpenAssert.Executable executable) Asserts that executable throws expected exception 断言可执行对象抛出预期异常- Type Parameters:
T- the exception type | 异常类型- Parameters:
expectedType- the expected exception type | 期望异常类型executable- the executable | 可执行对象- Returns:
- the thrown exception | 抛出的异常
-
assertDoesNotThrow
Asserts that executable does not throw 断言可执行对象不抛出异常- Parameters:
executable- the executable | 可执行对象
-
assertTimeout
Asserts that executable completes within timeout 断言可执行对象在超时内完成- Parameters:
timeout- the timeout duration | 超时时间executable- the executable | 可执行对象
-
fail
public static void fail()Fails unconditionally 无条件失败 -
fail
Fails unconditionally with message 无条件失败(带消息)- Parameters:
message- the message | 消息
-