Class ExceptionAssert
java.lang.Object
cloud.opencode.base.test.assertion.ExceptionAssert
Exception Assert - Fluent assertions for exceptions
异常断言 - 异常的流式断言
Provides comprehensive assertion methods for exception testing.
为异常测试提供全面的断言方法。
Features | 主要功能:
- Assert exception type, message, and cause - 断言异常类型、消息和原因
- Assert code does not throw - 断言代码不抛出异常
- Root cause inspection - 根因检查
- Fluent chaining API - 流式链式API
Usage Examples | 使用示例:
ExceptionAssert.assertThatThrownBy(() -> {
throw new IllegalArgumentException("Invalid input");
})
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid input")
.hasNoCause();
ExceptionAssert.assertThatCode(() -> {
// safe code
})
.doesNotThrowAnyException();
Security | 安全性:
- Thread-safe: No (not designed for concurrent use) - 线程安全: 否(非设计用于并发使用)
- Null-safe: Yes (handles null throwable gracefully) - 空值安全: 是(优雅处理空异常)
- Since:
- JDK 25, opencode-base-test V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for code that may throw. -
Method Summary
Modifier and TypeMethodDescriptionstatic ExceptionAssertassertThat(Throwable throwable) Creates assertion for exception.static ExceptionAssertCreates assertion for code that should not throw.static ExceptionAssertCreates assertion expecting exception to be thrown.Asserts that no exception is thrown.<T extends Throwable>
TGets the thrown exception for further inspection.hasCause()Asserts that exception has cause.hasCauseInstanceOf(Class<? extends Throwable> causeType) Asserts that exception has cause of type.hasMessage(String expectedMessage) Asserts that exception has message.hasMessageContaining(String substring) Asserts that exception message contains.hasMessageEndingWith(String suffix) Asserts that exception message ends with.hasMessageStartingWith(String prefix) Asserts that exception message starts with.Asserts that exception has no cause.hasRootCauseInstanceOf(Class<? extends Throwable> rootCauseType) Asserts that exception has root cause of type.isExactlyInstanceOf(Class<? extends Throwable> expectedType) Asserts that exception is exactly of type.isInstanceOf(Class<? extends Throwable> expectedType) Asserts that exception is instance of.
-
Method Details
-
assertThatThrownBy
Creates assertion expecting exception to be thrown. 创建期望抛出异常的断言。- Parameters:
runner- the code to execute | 要执行的代码- Returns:
- the assertion | 断言
-
assertThat
Creates assertion for exception. 为异常创建断言。- Parameters:
throwable- the throwable | 异常- Returns:
- the assertion | 断言
-
assertThatCode
Creates assertion for code that should not throw. 创建不应抛出异常的代码的断言。- Parameters:
runner- the code to execute | 要执行的代码- Returns:
- the assertion | 断言
-
doesNotThrowAnyException
Asserts that no exception is thrown. 断言不抛出异常。- Returns:
- this | 此对象
-
isInstanceOf
Asserts that exception is instance of. 断言异常是指定类型。- Parameters:
expectedType- the expected type | 期望类型- Returns:
- this | 此对象
-
isExactlyInstanceOf
Asserts that exception is exactly of type. 断言异常恰好是指定类型。- Parameters:
expectedType- the expected type | 期望类型- Returns:
- this | 此对象
-
hasMessage
Asserts that exception has message. 断言异常有指定消息。- Parameters:
expectedMessage- the expected message | 期望消息- Returns:
- this | 此对象
-
hasMessageContaining
Asserts that exception message contains. 断言异常消息包含。- Parameters:
substring- the substring | 子字符串- Returns:
- this | 此对象
-
hasMessageStartingWith
Asserts that exception message starts with. 断言异常消息以...开始。- Parameters:
prefix- the prefix | 前缀- Returns:
- this | 此对象
-
hasMessageEndingWith
Asserts that exception message ends with. 断言异常消息以...结束。- Parameters:
suffix- the suffix | 后缀- Returns:
- this | 此对象
-
hasCause
-
hasNoCause
Asserts that exception has no cause. 断言异常没有原因。- Returns:
- this | 此对象
-
hasCauseInstanceOf
Asserts that exception has cause of type. 断言异常有指定类型的原因。- Parameters:
causeType- the cause type | 原因类型- Returns:
- this | 此对象
-
hasRootCauseInstanceOf
Asserts that exception has root cause of type. 断言异常有指定类型的根原因。- Parameters:
rootCauseType- the root cause type | 根原因类型- Returns:
- this | 此对象
-
getThrowable
Gets the thrown exception for further inspection. 获取抛出的异常以进行进一步检查。- Type Parameters:
T- the expected type | 期望类型- Returns:
- the throwable | 异常
-