Class AbstractCompletableFutureAssert<SELF extends AbstractCompletableFutureAssert<SELF,RESULT>,RESULT>
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<SELF,CompletableFuture<RESULT>>
-
- org.assertj.core.api.AbstractCompletableFutureAssert<SELF,RESULT>
-
- Type Parameters:
RESULT
- type of the value contained in theCompletableFuture
.
- All Implemented Interfaces:
Assert<SELF,CompletableFuture<RESULT>>
,Descriptable<SELF>
,ExtensionPoints<SELF,CompletableFuture<RESULT>>
- Direct Known Subclasses:
CompletableFutureAssert
public abstract class AbstractCompletableFutureAssert<SELF extends AbstractCompletableFutureAssert<SELF,RESULT>,RESULT> extends AbstractAssert<SELF,CompletableFuture<RESULT>>
Assertions forCompletableFuture
.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) Futures
futures
-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, assertionErrorCreator, conditions, info, myself, objects, throwUnsupportedExceptionOnEquals
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractCompletableFutureAssert(CompletableFuture<RESULT> actual, Class<?> selfType)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SELF
hasFailed()
Verifies that theCompletableFuture
has completed exceptionally but has not been cancelled, this assertion is equivalent to:AbstractThrowableAssert<?,? extends Throwable>
hasFailedWithThrowableThat()
Verifies that theCompletableFuture
has completed exceptionally and returns a Throwable assertion object allowing to check the Throwable that has caused the future to fail.SELF
hasNotFailed()
Verifies that theCompletableFuture
has not failed i.e: incomplete, completed or cancelled.
This is different fromisNotCompletedExceptionally()
as a cancelled future has not failed but is completed exceptionally.private ObjectAssert<RESULT>
internalSucceedsWithin(long timeout, TimeUnit unit)
private ObjectAssert<RESULT>
internalSucceedsWithin(Duration timeout)
SELF
isCancelled()
Verifies that theCompletableFuture
is cancelled.SELF
isCompleted()
Verifies that theCompletableFuture
is completed normally (i.e.done
but notcompleted exceptionally
) orcancelled
).SELF
isCompletedExceptionally()
Verifies that theCompletableFuture
is completed exceptionally.SELF
isCompletedWithValue(RESULT expected)
Verifies that theCompletableFuture
is completed normally with theexpected
result.SELF
isCompletedWithValueMatching(Predicate<? super RESULT> predicate)
Verifies that theCompletableFuture
is completed normally with a result matching thepredicate
.SELF
isCompletedWithValueMatching(Predicate<? super RESULT> predicate, String description)
Verifies that theCompletableFuture
is completed normally with a result matching thepredicate
, the String parameter is used in the error message.private SELF
isCompletedWithValueMatching(Predicate<? super RESULT> predicate, PredicateDescription description)
SELF
isDone()
Verifies that theCompletableFuture
is done i.e.SELF
isNotCancelled()
Verifies that theCompletableFuture
is not cancelled.SELF
isNotCompleted()
Verifies that theCompletableFuture
is not completed normally (i.e.SELF
isNotCompletedExceptionally()
Verifies that theCompletableFuture
is not completed exceptionally.SELF
isNotDone()
Verifies that theCompletableFuture
is not done.ObjectAssert<RESULT>
succeedsWithin(long timeout, TimeUnit unit)
Waits if necessary for at most the given time for this future to complete, and then returns its result for further assertions.<ASSERT extends AbstractAssert<?,?>>
ASSERTsucceedsWithin(long timeout, TimeUnit unit, InstanceOfAssertFactory<RESULT,ASSERT> assertFactory)
Waits if necessary for at most the given time for this future to complete, theInstanceOfAssertFactory
parameter is used to return assertions specific to the the future's result type.ObjectAssert<RESULT>
succeedsWithin(Duration timeout)
Waits if necessary for at most the given time for this future to complete, and then returns its result for further assertions.<ASSERT extends AbstractAssert<?,?>>
ASSERTsucceedsWithin(Duration timeout, InstanceOfAssertFactory<RESULT,ASSERT> assertFactory)
Waits if necessary for at most the given time for this future to complete, theInstanceOfAssertFactory
parameter is used to return assertions specific to the the future's result type.-
Methods inherited from class org.assertj.core.api.AbstractAssert
asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, extracting, extracting, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, inBinary, inHexadecimal, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, usingRecursiveComparison, usingRecursiveComparison, withAssertionState, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnError
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.assertj.core.api.Descriptable
as, as, describedAs
-
-
-
-
Field Detail
-
futures
Futures futures
-
-
Constructor Detail
-
AbstractCompletableFutureAssert
protected AbstractCompletableFutureAssert(CompletableFuture<RESULT> actual, Class<?> selfType)
-
-
Method Detail
-
isDone
public SELF isDone()
Verifies that theCompletableFuture
is done i.e. completed normally, exceptionally, or via cancellation.Assertion will pass :
Assertion will fail :assertThat(CompletableFuture.completedFuture("something")).isDone();
assertThat(new CompletableFuture()).isDone();
- Returns:
- this assertion object.
- See Also:
CompletableFuture.isDone()
-
isNotDone
public SELF isNotDone()
Verifies that theCompletableFuture
is not done.Assertion will pass :
Assertion will fail :assertThat(new CompletableFuture()).isNotDone();
assertThat(CompletableFuture.completedFuture("something")).isNotDone();
- Returns:
- this assertion object.
- See Also:
CompletableFuture.isDone()
-
isCompletedExceptionally
public SELF isCompletedExceptionally()
Verifies that theCompletableFuture
is completed exceptionally. Possible causes include cancellation, explicit invocation of completeExceptionally, and abrupt termination of a CompletionStage action.If you only want to check that actual future is completed exceptionally but not cancelled, use
hasFailed()
orhasFailedWithThrowableThat()
.Assertion will pass :
Assertion will fail :CompletableFuture future = new CompletableFuture(); future.completeExceptionally(new RuntimeException()); assertThat(future).isCompletedExceptionally();
assertThat(CompletableFuture.completedFuture("something")).isCompletedExceptionally();
- Returns:
- this assertion object.
- See Also:
CompletableFuture.isCompletedExceptionally()
-
isNotCompletedExceptionally
public SELF isNotCompletedExceptionally()
Verifies that theCompletableFuture
is not completed exceptionally.Assertion will pass :
Assertion will fail :assertThat(CompletableFuture.completedFuture("something")).isNotCompletedExceptionally();
CompletableFuture future = new CompletableFuture(); future.completeExceptionally(new RuntimeException()); assertThat(future).isNotCompletedExceptionally();
- Returns:
- this assertion object.
- See Also:
CompletableFuture.isCompletedExceptionally()
-
isCancelled
public SELF isCancelled()
Verifies that theCompletableFuture
is cancelled.Assertion will pass :
Assertion will fail :CompletableFuture future = new CompletableFuture(); future.cancel(true); assertThat(future).isCancelled();
assertThat(new CompletableFuture()).isCancelled();
- Returns:
- this assertion object.
- See Also:
CompletableFuture.isCancelled()
-
isNotCancelled
public SELF isNotCancelled()
Verifies that theCompletableFuture
is not cancelled.Assertion will pass :
Assertion will fail :assertThat(new CompletableFuture()).isNotCancelled();
CompletableFuture future = new CompletableFuture(); future.cancel(true); assertThat(future).isNotCancelled();
- Returns:
- this assertion object.
- See Also:
CompletableFuture.isCancelled()
-
isCompleted
public SELF isCompleted()
Verifies that theCompletableFuture
is completed normally (i.e.done
but notcompleted exceptionally
) orcancelled
).Assertion will pass :
Assertion will fail :assertThat(CompletableFuture.completedFuture("something")).isCompleted();
assertThat(new CompletableFuture()).isCompleted();
- Returns:
- this assertion object.
-
isNotCompleted
public SELF isNotCompleted()
Verifies that theCompletableFuture
is not completed normally (i.e. incomplete, failed or cancelled).Assertion will pass :
Assertion will fail :assertThat(new CompletableFuture()).isNotCompleted();
assertThat(CompletableFuture.completedFuture("something")).isNotCompleted();
- Returns:
- this assertion object.
-
isCompletedWithValue
public SELF isCompletedWithValue(RESULT expected)
Verifies that theCompletableFuture
is completed normally with theexpected
result.Assertion will pass :
Assertion will fail :assertThat(CompletableFuture.completedFuture("something")) .isCompletedWithValue("something");
assertThat(CompletableFuture.completedFuture("something")) .isCompletedWithValue("something else");
- Parameters:
expected
- the expected result value of theCompletableFuture
.- Returns:
- this assertion object.
-
isCompletedWithValueMatching
public SELF isCompletedWithValueMatching(Predicate<? super RESULT> predicate)
Verifies that theCompletableFuture
is completed normally with a result matching thepredicate
.Assertion will pass :
Assertion will fail :assertThat(CompletableFuture.completedFuture("something")) .isCompletedWithValueMatching(result -> result.equals("something"));
assertThat(CompletableFuture.completedFuture("something")) .isCompletedWithValueMatching(result -> result.equals("something else"));
- Parameters:
predicate
- thePredicate
to apply.- Returns:
- this assertion object.
-
isCompletedWithValueMatching
public SELF isCompletedWithValueMatching(Predicate<? super RESULT> predicate, String description)
Verifies that theCompletableFuture
is completed normally with a result matching thepredicate
, the String parameter is used in the error message.Assertion will pass :
Assertion will fail :assertThat(CompletableFuture.completedFuture("something")) .isCompletedWithValueMatching(result -> result != null, "expected not null");
Error message is:assertThat(CompletableFuture.completedFuture("something")) .isCompletedWithValueMatching(result -> result == null, "expected null");
Expecting: <"something"> to match 'expected null' predicate.
-
isCompletedWithValueMatching
private SELF isCompletedWithValueMatching(Predicate<? super RESULT> predicate, PredicateDescription description)
-
hasFailed
public SELF hasFailed()
Verifies that theCompletableFuture
has completed exceptionally but has not been cancelled, this assertion is equivalent to:assertThat(future).isCompletedExceptionally() .isNotCancelled();
Assertion will pass :
Assertion will fail :CompletableFuture future = new CompletableFuture(); future.completeExceptionally(new RuntimeException()); assertThat(future).hasFailed();
CompletableFuture future = new CompletableFuture(); future.cancel(true); assertThat(future).hasFailed();
- Returns:
- this assertion object.
-
hasNotFailed
public SELF hasNotFailed()
Verifies that theCompletableFuture
has not failed i.e: incomplete, completed or cancelled.
This is different fromisNotCompletedExceptionally()
as a cancelled future has not failed but is completed exceptionally.Assertion will pass :
Assertion will fail :CompletableFuture future = new CompletableFuture(); future.cancel(true); assertThat(future).hasNotFailed();
CompletableFuture future = new CompletableFuture(); future.completeExceptionally(new RuntimeException()); assertThat(future).hasNotFailed();
- Returns:
- this assertion object.
-
succeedsWithin
public ObjectAssert<RESULT> succeedsWithin(Duration timeout)
Waits if necessary for at most the given time for this future to complete, and then returns its result for further assertions.If the future's result is not available for any reason an assertion error is thrown.
To get assertions for the future result's type use
succeedsWithin(Duration, InstanceOfAssertFactory)
instead.Examples:
CompletableFuture<String> future = CompletableFuture.completedFuture("ook!"); Duration timeout = Duration.ofMillis(100); // assertion succeeds assertThat(future).succeedsWithin(timeout) .isEqualTo("ook!"); // fails assuming the future is not done after the given timeout CompletableFuture<String> future = ... ; // future too long to complete assertThat(future).succeedsWithin(timeout); // fails as the future is cancelled CompletableFuture future = new CompletableFuture(); future.cancel(false); assertThat(future).succeedsWithin(timeout);
- Parameters:
timeout
- the maximum time to wait- Returns:
- a new assertion object on the the future's result.
- Throws:
AssertionError
- if the actualCompletableFuture
isnull
.AssertionError
- if the actualCompletableFuture
does not succeed within the given timeout.
-
internalSucceedsWithin
private ObjectAssert<RESULT> internalSucceedsWithin(Duration timeout)
-
succeedsWithin
public ObjectAssert<RESULT> succeedsWithin(long timeout, TimeUnit unit)
Waits if necessary for at most the given time for this future to complete, and then returns its result for further assertions.If the future's result is not available for any reason an assertion error is thrown.
To get assertions for the future result's type use
succeedsWithin(long, TimeUnit, InstanceOfAssertFactory)
instead.Examples:
CompletableFuture<String> future = CompletableFuture.completedFuture("ook!"); // assertion succeeds assertThat(future).succeedsWithin(100, TimeUnit.MILLISECONDS) .isEqualTo("ook!"); // fails assuming the future is not done after the given timeout CompletableFuture<String> future = ... ; // future too long to complete assertThat(future).succeedsWithin(100, TimeUnit.MILLISECONDS); // fails as the future is cancelled CompletableFuture future = new CompletableFuture(); future.cancel(false); assertThat(future).succeedsWithin(100, TimeUnit.MILLISECONDS);
- Parameters:
timeout
- the maximum time to waitunit
- the time unit of the timeout argument- Returns:
- a new assertion object on the the future's result.
- Throws:
AssertionError
- if the actualCompletableFuture
isnull
.AssertionError
- if the actualCompletableFuture
does not succeed within the given timeout.
-
internalSucceedsWithin
private ObjectAssert<RESULT> internalSucceedsWithin(long timeout, TimeUnit unit)
-
succeedsWithin
public <ASSERT extends AbstractAssert<?,?>> ASSERT succeedsWithin(Duration timeout, InstanceOfAssertFactory<RESULT,ASSERT> assertFactory)
Waits if necessary for at most the given time for this future to complete, theInstanceOfAssertFactory
parameter is used to return assertions specific to the the future's result type.If the future's result is not available for any reason an assertion error is thrown.
Examples:
CompletableFuture<String> future = CompletableFuture.completedFuture("ook!"); Duration timeout = Duration.ofMillis(100); // assertion succeeds // using asInstanceOf is recommended to get assertions for the future result's type assertThat(future).succeedsWithin(timeout, InstanceOfAssertFactories.STRING) .contains("ok"); // assertion fails if the narrowed type for assertions is incompatible with the future's result type. assertThat(future).succeedsWithin(timeout, InstanceOfAssertFactories.DATE) .isToday();
- Type Parameters:
ASSERT
- the type of the resultingAssert
- Parameters:
timeout
- the maximum time to waitassertFactory
- the factory which verifies the type and creates the newAssert
- Returns:
- a new narrowed
Assert
instance for assertions chaining on the value of theCompletableFuture
- Throws:
AssertionError
- if the actualCompletableFuture
isnull
.IllegalStateException
- if the actualCompletableFuture
does not succeed within the given timeout.
-
succeedsWithin
public <ASSERT extends AbstractAssert<?,?>> ASSERT succeedsWithin(long timeout, TimeUnit unit, InstanceOfAssertFactory<RESULT,ASSERT> assertFactory)
Waits if necessary for at most the given time for this future to complete, theInstanceOfAssertFactory
parameter is used to return assertions specific to the the future's result type.If the future's result is not available for any reason an assertion error is thrown.
Examples:
CompletableFuture<String> future = CompletableFuture.completedFuture("ook!"); // assertion succeeds // using asInstanceOf is recommended to get assertions for the future result's type assertThat(future).succeedsWithin(100, TimeUnit.MILLISECONDS, InstanceOfAssertFactories.STRING) .contains("ok"); // assertion fails if the narrowed type for assertions is incompatible with the future's result type. assertThat(future).succeedsWithin(100, TimeUnit.MILLISECONDS, InstanceOfAssertFactories.DATE) .isToday();
- Type Parameters:
ASSERT
- the type of the resultingAssert
- Parameters:
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentassertFactory
- the factory which verifies the type and creates the newAssert
- Returns:
- a new narrowed
Assert
instance for assertions chaining on the value of theCompletableFuture
- Throws:
AssertionError
- if the actualCompletableFuture
isnull
.AssertionError
- if the actualCompletableFuture
does not succeed within the given timeout.
-
hasFailedWithThrowableThat
public AbstractThrowableAssert<?,? extends Throwable> hasFailedWithThrowableThat()
Verifies that theCompletableFuture
has completed exceptionally and returns a Throwable assertion object allowing to check the Throwable that has caused the future to fail.Assertion will pass :
Assertion will fail :CompletableFuture future = new CompletableFuture(); future.completeExceptionally(new RuntimeException("boom!")); assertThat(future).hasFailedWithThrowableThat().isInstanceOf(RuntimeException.class); .hasMessage("boom!");
CompletableFuture future = new CompletableFuture(); future.completeExceptionally(new RuntimeException()); assertThat(future).hasFailedWithThrowableThat().isInstanceOf(IllegalArgumentException.class);
- Returns:
- an exception assertion object.
-
-