Class AbstractIteratorAssert<SELF extends AbstractIteratorAssert<SELF,​ELEMENT>,​ELEMENT>

  • Type Parameters:
    SELF - the "self" type of this assertion class.
    ELEMENT - the type of elements.
    All Implemented Interfaces:
    Assert<SELF,​java.util.Iterator<? extends ELEMENT>>, Descriptable<SELF>, ExtensionPoints<SELF,​java.util.Iterator<? extends ELEMENT>>
    Direct Known Subclasses:
    IteratorAssert

    public abstract class AbstractIteratorAssert<SELF extends AbstractIteratorAssert<SELF,​ELEMENT>,​ELEMENT>
    extends AbstractAssert<SELF,​java.util.Iterator<? extends ELEMENT>>

    Base class for all implementations of assertions for Iterators.

    Note that none of the assertions modify the actual iterator, i.e. they do not consume any elements. In order to use consuming assertions, use toIterable().

    Since:
    3.12.0
    • Constructor Detail

      • AbstractIteratorAssert

        protected AbstractIteratorAssert​(java.util.Iterator<? extends ELEMENT> actual,
                                         java.lang.Class<?> selfType)
        Creates a new AbstractIteratorAssert.
        Parameters:
        actual - the actual value to verify
        selfType - the "self type"
    • Method Detail

      • hasNext

        public SELF hasNext()

        Verifies that the actual Iterator has at least one more element.

        Example:
         Iterator<TolkienCharacter> elvesRingBearers = list(galadriel, elrond, gandalf).iterator();
        
         assertThat(elvesRingBearers).hasNext();
        Returns:
        this assertion object.
        Throws:
        java.lang.AssertionError - if the actual Iterator is null or does not have another element.
        Since:
        3.12.0
      • isExhausted

        public SELF isExhausted()

        Verifies that the actual Iterator has no more elements.

        Example:
         Iterator<String> result = Collections.emptyList().iterator();
        
         assertThat(result).isExhausted();
        Returns:
        this assertion object.
        Throws:
        java.lang.AssertionError - if the actual Iterator is null or has another element.
        Since:
        3.12.0
      • isUnmodifiable

        public SELF isUnmodifiable()
        Verifies that the actual iterator is unmodifiable, i.e., throws an UnsupportedOperationException with any attempt to remove from the iterator.

        Example:

         // assertions will pass
         assertThat(List.of().iterator()).isUnmodifiable();
         assertThat(Set.of().iterator()).isUnmodifiable();
        
         // assertions will fail
         assertThat(new ArrayList<>().iterator()).isUnmodifiable();
         assertThat(new HashSet<>().iterator()).isUnmodifiable();
        Returns:
        this assertion object.
        Throws:
        java.lang.AssertionError - if the actual iterator is modifiable.
        Since:
        3.26.0
      • assertIsUnmodifiable

        private void assertIsUnmodifiable()
      • expectUnsupportedOperationException

        private void expectUnsupportedOperationException​(java.lang.Runnable runnable,
                                                         java.lang.String method)