Class AssertConsumeCount<T>

java.lang.Object
org.soliscode.test.assertions.actions.AssertConsumeCount<T>
Type Parameters:
T - element type
All Implemented Interfaces:
Consumer<T>, AssertAction, AssertConsumer<T>

public class AssertConsumeCount<T> extends Object implements AssertConsumer<T>

A consumer that counts each time it consumes an object and asserts that it has consumed the expected number of objects when done. Instead of instantiating this class directly, the AssertActions utility class should be used:

Consumer<Integer> consumer = AssertActions.consumeCount(3);

This assertion action must be checked after the method it is passed to has completed, so the assertCheck() method will need to be explicitly called:

AssertConsumer<Integer> consumer = AssertActions.consumeCount(7);
collection.forEach(consumer);
consumer.assertCheck();
Since:
1.0
Author:
evanbergstrom
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    AssertConsumeCount(int expected)
    Create a consumer that expect to consume a specified number of objects.
    AssertConsumeCount(int expected, @Nullable String message)
    Create a consumer that expect to consume a specified number of objects.
    AssertConsumeCount(int expected, @Nullable Supplier<String> messageSupplier)
    Create a consumer that expect to consume a specified number of objects.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    accept(T obj)
    Increments the count of the number of times the consumer has been used.
    void
    Asserts that the consumer has been called the expected number of times.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.util.function.Consumer

    andThen
  • Constructor Details

    • AssertConsumeCount

      public AssertConsumeCount(int expected)
      Create a consumer that expect to consume a specified number of objects.
      Parameters:
      expected - the number of objects to expect.
    • AssertConsumeCount

      public AssertConsumeCount(int expected, @Nullable @Nullable String message)
      Create a consumer that expect to consume a specified number of objects. This constructor allows specification of a string to be included in the exception of the assertion fails.
      Parameters:
      expected - the number of objects to expect.
      message - the text to include in the exception.
    • AssertConsumeCount

      public AssertConsumeCount(int expected, @Nullable @Nullable Supplier<String> messageSupplier)
      Create a consumer that expect to consume a specified number of objects. This constructor allows specification of a supplier of a message to be included in the exception.
      Parameters:
      expected - the number of objects to expect.
      messageSupplier - the supplier of the text to include in the exception.
  • Method Details

    • accept

      public void accept(@Nullable T obj)
      Increments the count of the number of times the consumer has been used.
      Specified by:
      accept in interface Consumer<T>
      Parameters:
      obj - the object that is being consumed.
    • assertCheck

      public void assertCheck()
      Asserts that the consumer has been called the expected number of times.
      Specified by:
      assertCheck in interface AssertAction
      Throws:
      org.opentest4j.AssertionFailedError - if the consumer has not been called the expected number of times.