Class CollectionAssert<E>

java.lang.Object
cloud.opencode.base.test.assertion.CollectionAssert<E>
Type Parameters:
E - the element type | 元素类型

public final class CollectionAssert<E> extends Object
Collection Assert - Fluent assertions for collections 集合断言 - 集合的流式断言

Provides comprehensive assertion methods for Collection types including List, Set, and other Collection implementations.

为Collection类型提供全面的断言方法,包括List、Set和其他Collection实现。

Features | 主要功能:

  • Fluent assertion API for collections - 集合的流式断言API
  • Size, containment, ordering checks - 大小、包含、排序检查
  • Predicate-based matching (allMatch, anyMatch, noneMatch) - 基于谓词的匹配
  • Duplicate detection - 重复检测

Usage Examples | 使用示例:

CollectionAssert.assertThat(list)
    .isNotEmpty()
    .hasSize(3)
    .contains("apple")
    .containsAll("apple", "banana")
    .doesNotContain("cherry");

Security | 安全性:

  • Thread-safe: No (not designed for concurrent use) - 线程安全: 否(非设计用于并发使用)
  • Null-safe: Yes (validates non-null collection) - 空值安全: 是(验证非空集合)
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • assertThat

      public static <E> CollectionAssert<E> assertThat(Collection<E> actual)
      Creates assertion for collection. 为集合创建断言。
      Type Parameters:
      E - the element type | 元素类型
      Parameters:
      actual - the actual collection | 实际集合
      Returns:
      the assertion | 断言
    • isNull

      public CollectionAssert<E> isNull()
      Asserts that collection is null. 断言集合为null。
      Returns:
      this | 此对象
    • isNotNull

      public CollectionAssert<E> isNotNull()
      Asserts that collection is not null. 断言集合不为null。
      Returns:
      this | 此对象
    • isEmpty

      public CollectionAssert<E> isEmpty()
      Asserts that collection is empty. 断言集合为空。
      Returns:
      this | 此对象
    • isNotEmpty

      public CollectionAssert<E> isNotEmpty()
      Asserts that collection is not empty. 断言集合不为空。
      Returns:
      this | 此对象
    • hasSize

      public CollectionAssert<E> hasSize(int expectedSize)
      Asserts that collection has specified size. 断言集合有指定大小。
      Parameters:
      expectedSize - the expected size | 期望大小
      Returns:
      this | 此对象
    • hasSizeGreaterThan

      public CollectionAssert<E> hasSizeGreaterThan(int size)
      Asserts that collection has size greater than. 断言集合大小大于。
      Parameters:
      size - the size | 大小
      Returns:
      this | 此对象
    • hasSizeLessThan

      public CollectionAssert<E> hasSizeLessThan(int size)
      Asserts that collection has size less than. 断言集合大小小于。
      Parameters:
      size - the size | 大小
      Returns:
      this | 此对象
    • contains

      public CollectionAssert<E> contains(E element)
      Asserts that collection contains element. 断言集合包含元素。
      Parameters:
      element - the element | 元素
      Returns:
      this | 此对象
    • doesNotContain

      public CollectionAssert<E> doesNotContain(E element)
      Asserts that collection does not contain element. 断言集合不包含元素。
      Parameters:
      element - the element | 元素
      Returns:
      this | 此对象
    • containsAll

      @SafeVarargs public final CollectionAssert<E> containsAll(E... elements)
      Asserts that collection contains all elements. 断言集合包含所有元素。
      Parameters:
      elements - the elements | 元素
      Returns:
      this | 此对象
    • containsExactly

      @SafeVarargs public final CollectionAssert<E> containsExactly(E... elements)
      Asserts that collection contains exactly the given elements. 断言集合恰好包含给定元素。
      Parameters:
      elements - the elements | 元素
      Returns:
      this | 此对象
    • containsExactlyInAnyOrder

      @SafeVarargs public final CollectionAssert<E> containsExactlyInAnyOrder(E... elements)
      Asserts that collection contains exactly the given elements in any order. 断言集合恰好包含给定元素(任意顺序)。
      Parameters:
      elements - the elements | 元素
      Returns:
      this | 此对象
    • allMatch

      public CollectionAssert<E> allMatch(Predicate<E> predicate)
      Asserts that collection contains only elements matching predicate. 断言集合只包含匹配谓词的元素。
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      this | 此对象
    • anyMatch

      public CollectionAssert<E> anyMatch(Predicate<E> predicate)
      Asserts that collection contains any element matching predicate. 断言集合包含任何匹配谓词的元素。
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      this | 此对象
    • noneMatch

      public CollectionAssert<E> noneMatch(Predicate<E> predicate)
      Asserts that collection contains no element matching predicate. 断言集合不包含匹配谓词的元素。
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      this | 此对象
    • hasNoDuplicates

      public CollectionAssert<E> hasNoDuplicates()
      Asserts that collection has no duplicates. 断言集合没有重复元素。
      Returns:
      this | 此对象
    • isSorted

      public CollectionAssert<E> isSorted(Comparator<E> comparator)
      Asserts that collection is sorted. 断言集合已排序。
      Parameters:
      comparator - the comparator | 比较器
      Returns:
      this | 此对象
    • isEqualTo

      public CollectionAssert<E> isEqualTo(Collection<E> expected)
      Asserts that collection equals another. 断言集合等于另一个。
      Parameters:
      expected - the expected collection | 期望集合
      Returns:
      this | 此对象