Class ExceptionAssert

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

public final class ExceptionAssert extends Object
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:
  • Method Details

    • assertThatThrownBy

      public static ExceptionAssert assertThatThrownBy(ExceptionAssert.ThrowableRunner runner)
      Creates assertion expecting exception to be thrown. 创建期望抛出异常的断言。
      Parameters:
      runner - the code to execute | 要执行的代码
      Returns:
      the assertion | 断言
    • assertThat

      public static ExceptionAssert assertThat(Throwable throwable)
      Creates assertion for exception. 为异常创建断言。
      Parameters:
      throwable - the throwable | 异常
      Returns:
      the assertion | 断言
    • assertThatCode

      public static ExceptionAssert assertThatCode(ExceptionAssert.ThrowableRunner runner)
      Creates assertion for code that should not throw. 创建不应抛出异常的代码的断言。
      Parameters:
      runner - the code to execute | 要执行的代码
      Returns:
      the assertion | 断言
    • doesNotThrowAnyException

      public ExceptionAssert doesNotThrowAnyException()
      Asserts that no exception is thrown. 断言不抛出异常。
      Returns:
      this | 此对象
    • isInstanceOf

      public ExceptionAssert isInstanceOf(Class<? extends Throwable> expectedType)
      Asserts that exception is instance of. 断言异常是指定类型。
      Parameters:
      expectedType - the expected type | 期望类型
      Returns:
      this | 此对象
    • isExactlyInstanceOf

      public ExceptionAssert isExactlyInstanceOf(Class<? extends Throwable> expectedType)
      Asserts that exception is exactly of type. 断言异常恰好是指定类型。
      Parameters:
      expectedType - the expected type | 期望类型
      Returns:
      this | 此对象
    • hasMessage

      public ExceptionAssert hasMessage(String expectedMessage)
      Asserts that exception has message. 断言异常有指定消息。
      Parameters:
      expectedMessage - the expected message | 期望消息
      Returns:
      this | 此对象
    • hasMessageContaining

      public ExceptionAssert hasMessageContaining(String substring)
      Asserts that exception message contains. 断言异常消息包含。
      Parameters:
      substring - the substring | 子字符串
      Returns:
      this | 此对象
    • hasMessageStartingWith

      public ExceptionAssert hasMessageStartingWith(String prefix)
      Asserts that exception message starts with. 断言异常消息以...开始。
      Parameters:
      prefix - the prefix | 前缀
      Returns:
      this | 此对象
    • hasMessageEndingWith

      public ExceptionAssert hasMessageEndingWith(String suffix)
      Asserts that exception message ends with. 断言异常消息以...结束。
      Parameters:
      suffix - the suffix | 后缀
      Returns:
      this | 此对象
    • hasCause

      public ExceptionAssert hasCause()
      Asserts that exception has cause. 断言异常有原因。
      Returns:
      this | 此对象
    • hasNoCause

      public ExceptionAssert hasNoCause()
      Asserts that exception has no cause. 断言异常没有原因。
      Returns:
      this | 此对象
    • hasCauseInstanceOf

      public ExceptionAssert hasCauseInstanceOf(Class<? extends Throwable> causeType)
      Asserts that exception has cause of type. 断言异常有指定类型的原因。
      Parameters:
      causeType - the cause type | 原因类型
      Returns:
      this | 此对象
    • hasRootCauseInstanceOf

      public ExceptionAssert hasRootCauseInstanceOf(Class<? extends Throwable> rootCauseType)
      Asserts that exception has root cause of type. 断言异常有指定类型的根原因。
      Parameters:
      rootCauseType - the root cause type | 根原因类型
      Returns:
      this | 此对象
    • getThrowable

      public <T extends Throwable> T getThrowable()
      Gets the thrown exception for further inspection. 获取抛出的异常以进行进一步检查。
      Type Parameters:
      T - the expected type | 期望类型
      Returns:
      the throwable | 异常