Class OpenAssert

java.lang.Object
cloud.opencode.base.test.OpenAssert

public final class OpenAssert extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static interface 
    Executable functional interface for exception assertions 用于异常断言的可执行函数式接口
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends Comparable<T>>
    void
    assertBetween(T actual, T min, T max)
    Asserts that actual is between min and max (inclusive) 断言实际值在最小值和最大值之间(包含)
    static void
    Asserts that string is blank 断言字符串为空白
    static void
    assertContains(Object element, Collection<?> collection)
    Asserts that collection contains element 断言集合包含元素
    static void
    assertContains(String expected, String actual)
    Asserts that string contains substring 断言字符串包含子串
    static void
    assertContainsKey(Object key, Map<?,?> map)
    Asserts that map contains key 断言映射包含键
    static void
    Asserts that executable does not throw 断言可执行对象不抛出异常
    static void
    assertEmpty(Collection<?> collection)
    Asserts that collection is empty 断言集合为空
    static void
    assertEmpty(Map<?,?> map)
    Asserts that map is empty 断言映射为空
    static void
    assertEndsWith(String suffix, String actual)
    Asserts that string ends with suffix 断言字符串以后缀结束
    static void
    assertEquals(double expected, double actual, double delta)
    Asserts that doubles are equal within delta 断言双精度数在误差范围内相等
    static void
    assertEquals(Object expected, Object actual)
    Asserts that objects are equal 断言对象相等
    static void
    assertEquals(Object expected, Object actual, String message)
    Asserts that objects are equal with message 断言对象相等(带消息)
    static void
    assertFalse(boolean condition)
    Asserts that condition is false 断言条件为假
    static void
    assertFalse(boolean condition, String message)
    Asserts that condition is false with message 断言条件为假(带消息)
    static <T extends Comparable<T>>
    void
    assertGreaterThan(T actual, T expected)
    Asserts that actual is greater than expected 断言实际值大于期望值
    static <T extends Comparable<T>>
    void
    assertLessThan(T actual, T expected)
    Asserts that actual is less than expected 断言实际值小于期望值
    static void
    assertMatches(String regex, String actual)
    Asserts that string matches regex 断言字符串匹配正则表达式
    static void
    Asserts that string is not blank 断言字符串不为空白
    static void
    assertNotEmpty(Collection<?> collection)
    Asserts that collection is not empty 断言集合不为空
    static void
    assertNotEquals(Object unexpected, Object actual)
    Asserts that objects are not equal 断言对象不相等
    static void
    Asserts that object is not null 断言对象不为null
    static void
    assertNotNull(Object obj, String message)
    Asserts that object is not null with message 断言对象不为null(带消息)
    static void
    assertNotSame(Object unexpected, Object actual)
    Asserts that objects are not the same instance 断言对象不是相同实例
    static void
    Asserts that object is null 断言对象为null
    static void
    assertNull(Object obj, String message)
    Asserts that object is null with message 断言对象为null(带消息)
    static void
    assertSame(Object expected, Object actual)
    Asserts that objects are the same instance 断言对象是相同实例
    static void
    assertSize(int expected, Collection<?> collection)
    Asserts collection size 断言集合大小
    static void
    assertStartsWith(String prefix, String actual)
    Asserts that string starts with prefix 断言字符串以前缀开始
    static <T extends Throwable>
    T
    assertThrows(Class<T> expectedType, OpenAssert.Executable executable)
    Asserts that executable throws expected exception 断言可执行对象抛出预期异常
    static void
    Asserts that executable completes within timeout 断言可执行对象在超时内完成
    static void
    assertTrue(boolean condition)
    Asserts that condition is true 断言条件为真
    static void
    assertTrue(boolean condition, String message)
    Asserts that condition is true with message 断言条件为真(带消息)
    static void
    assertTrue(boolean condition, Supplier<String> messageSupplier)
    Asserts that condition is true with lazy message 断言条件为真(延迟消息)
    static void
    Fails unconditionally 无条件失败
    static void
    fail(String message)
    Fails unconditionally with message 无条件失败(带消息)

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • assertTrue

      public static void assertTrue(boolean condition)
      Asserts that condition is true 断言条件为真
      Parameters:
      condition - the condition | 条件
    • assertTrue

      public static void assertTrue(boolean condition, String message)
      Asserts that condition is true with message 断言条件为真(带消息)
      Parameters:
      condition - the condition | 条件
      message - the message | 消息
    • assertTrue

      public static void assertTrue(boolean condition, Supplier<String> messageSupplier)
      Asserts that condition is true with lazy message 断言条件为真(延迟消息)
      Parameters:
      condition - the condition | 条件
      messageSupplier - the message supplier | 消息提供者
    • assertFalse

      public static void assertFalse(boolean condition)
      Asserts that condition is false 断言条件为假
      Parameters:
      condition - the condition | 条件
    • assertFalse

      public static void assertFalse(boolean condition, String message)
      Asserts that condition is false with message 断言条件为假(带消息)
      Parameters:
      condition - the condition | 条件
      message - the message | 消息
    • assertNull

      public static void assertNull(Object obj)
      Asserts that object is null 断言对象为null
      Parameters:
      obj - the object | 对象
    • assertNull

      public static void assertNull(Object obj, String message)
      Asserts that object is null with message 断言对象为null(带消息)
      Parameters:
      obj - the object | 对象
      message - the message | 消息
    • assertNotNull

      public static void assertNotNull(Object obj)
      Asserts that object is not null 断言对象不为null
      Parameters:
      obj - the object | 对象
    • assertNotNull

      public static void assertNotNull(Object obj, String message)
      Asserts that object is not null with message 断言对象不为null(带消息)
      Parameters:
      obj - the object | 对象
      message - the message | 消息
    • assertEquals

      public static void assertEquals(Object expected, Object actual)
      Asserts that objects are equal 断言对象相等
      Parameters:
      expected - the expected value | 期望值
      actual - the actual value | 实际值
    • assertEquals

      public static void assertEquals(Object expected, Object actual, String message)
      Asserts that objects are equal with message 断言对象相等(带消息)
      Parameters:
      expected - the expected value | 期望值
      actual - the actual value | 实际值
      message - the message | 消息
    • assertNotEquals

      public static void assertNotEquals(Object unexpected, Object actual)
      Asserts that objects are not equal 断言对象不相等
      Parameters:
      unexpected - the unexpected value | 不期望值
      actual - the actual value | 实际值
    • assertSame

      public static void assertSame(Object expected, Object actual)
      Asserts that objects are the same instance 断言对象是相同实例
      Parameters:
      expected - the expected instance | 期望实例
      actual - the actual instance | 实际实例
    • assertNotSame

      public static void assertNotSame(Object unexpected, Object actual)
      Asserts that objects are not the same instance 断言对象不是相同实例
      Parameters:
      unexpected - the unexpected instance | 不期望实例
      actual - the actual instance | 实际实例
    • 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

      public static <T extends Comparable<T>> void assertGreaterThan(T actual, T expected)
      Asserts that actual is greater than expected 断言实际值大于期望值
      Type Parameters:
      T - the comparable type | 可比较类型
      Parameters:
      actual - the actual value | 实际值
      expected - the expected value | 期望值
    • assertLessThan

      public static <T extends Comparable<T>> void assertLessThan(T actual, T expected)
      Asserts that actual is less than expected 断言实际值小于期望值
      Type Parameters:
      T - the comparable type | 可比较类型
      Parameters:
      actual - the actual value | 实际值
      expected - the expected value | 期望值
    • assertBetween

      public static <T extends Comparable<T>> void assertBetween(T actual, T min, T max)
      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

      public static void assertEmpty(Collection<?> collection)
      Asserts that collection is empty 断言集合为空
      Parameters:
      collection - the collection | 集合
    • assertNotEmpty

      public static void assertNotEmpty(Collection<?> collection)
      Asserts that collection is not empty 断言集合不为空
      Parameters:
      collection - the collection | 集合
    • assertSize

      public static void assertSize(int expected, Collection<?> collection)
      Asserts collection size 断言集合大小
      Parameters:
      expected - the expected size | 期望大小
      collection - the collection | 集合
    • assertContains

      public static void assertContains(Object element, Collection<?> collection)
      Asserts that collection contains element 断言集合包含元素
      Parameters:
      element - the element | 元素
      collection - the collection | 集合
    • assertEmpty

      public static void assertEmpty(Map<?,?> map)
      Asserts that map is empty 断言映射为空
      Parameters:
      map - the map | 映射
    • assertContainsKey

      public static void assertContainsKey(Object key, Map<?,?> map)
      Asserts that map contains key 断言映射包含键
      Parameters:
      key - the key | 键
      map - the map | 映射
    • assertBlank

      public static void assertBlank(String str)
      Asserts that string is blank 断言字符串为空白
      Parameters:
      str - the string | 字符串
    • assertNotBlank

      public static void assertNotBlank(String str)
      Asserts that string is not blank 断言字符串不为空白
      Parameters:
      str - the string | 字符串
    • assertContains

      public static void assertContains(String expected, String actual)
      Asserts that string contains substring 断言字符串包含子串
      Parameters:
      expected - the expected substring | 期望子串
      actual - the actual string | 实际字符串
    • assertStartsWith

      public static void assertStartsWith(String prefix, String actual)
      Asserts that string starts with prefix 断言字符串以前缀开始
      Parameters:
      prefix - the prefix | 前缀
      actual - the actual string | 实际字符串
    • assertEndsWith

      public static void assertEndsWith(String suffix, String actual)
      Asserts that string ends with suffix 断言字符串以后缀结束
      Parameters:
      suffix - the suffix | 后缀
      actual - the actual string | 实际字符串
    • assertMatches

      public static void assertMatches(String regex, String actual)
      Asserts that string matches regex 断言字符串匹配正则表达式
      Parameters:
      regex - the regex pattern | 正则表达式
      actual - the actual string | 实际字符串
    • 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

      public static void assertDoesNotThrow(OpenAssert.Executable executable)
      Asserts that executable does not throw 断言可执行对象不抛出异常
      Parameters:
      executable - the executable | 可执行对象
    • assertTimeout

      public static void assertTimeout(Duration timeout, OpenAssert.Executable executable)
      Asserts that executable completes within timeout 断言可执行对象在超时内完成
      Parameters:
      timeout - the timeout duration | 超时时间
      executable - the executable | 可执行对象
    • fail

      public static void fail()
      Fails unconditionally 无条件失败
    • fail

      public static void fail(String message)
      Fails unconditionally with message 无条件失败(带消息)
      Parameters:
      message - the message | 消息