RESULT - type of the value contained in the CompletableFuture.public abstract class AbstractCompletableFutureAssert<SELF extends AbstractCompletableFutureAssert<SELF,RESULT>,RESULT> extends AbstractAssert<SELF,CompletableFuture<RESULT>>
CompletableFuture.actual, info, myself, objects, throwUnsupportedExceptionOnEquals| Modifier | Constructor and Description |
|---|---|
protected |
AbstractCompletableFutureAssert(CompletableFuture<RESULT> actual,
Class<?> selfType) |
| Modifier and Type | Method and Description |
|---|---|
WithThrowable |
failsWithin(Duration timeout)
Checks that the future does not complete within the given time (by calling
Future.get(long, TimeUnit)) and returns
the exception that caused the failure for further (exception) assertions, the exception can be any of
InterruptedException, ExecutionException, TimeoutException or CancellationException. |
WithThrowable |
failsWithin(long timeout,
TimeUnit unit)
Checks that the future does not complete within the given time (by calling
Future.get(long, TimeUnit)) and returns
the exception that caused the failure for further (exception) assertions, the exception can be any of
InterruptedException, ExecutionException, TimeoutException or CancellationException. |
SELF |
hasFailed()
Deprecated.
Combine isCompletedExceptionally with isNotCancelled instead:
This assertion is deprecated to change the semantics of failed to correspond to CompletableFuture.get() failing.
Original javadoc
Verifies that the
Assertion will pass :
Assertion will fail :
|
AbstractThrowableAssert<?,? extends Throwable> |
hasFailedWithThrowableThat()
Deprecated.
Although not 100% the same, consider using
This assertion is deprecated because it relies on hasFailed() semantics which we want to move away from (they
are not clear!) and to use failure semantics corresponding to CompletableFuture.get() failing.
Original javadoc
Verifies that the Assertion will pass :
Assertion will fail :
|
SELF |
hasNotFailed()
Deprecated.
Use matches with the following combination instead:
This assertion is deprecated because its semantic is not obvious.
Original javadoc
Verifies that the Assertion will pass :
Assertion will fail :
|
SELF |
isCancelled()
Verifies that the
CompletableFuture is cancelled. |
SELF |
isCompleted()
Verifies that the
CompletableFuture is completed normally (i.e.done but not
completed exceptionally) or cancelled. |
SELF |
isCompletedExceptionally()
Verifies that the
CompletableFuture is completed exceptionally. |
SELF |
isCompletedWithValue(RESULT expected)
Verifies that the
CompletableFuture is completed normally with the expected result. |
SELF |
isCompletedWithValueMatching(Predicate<? super RESULT> predicate)
Verifies that the
CompletableFuture is completed normally with a result matching the predicate. |
SELF |
isCompletedWithValueMatching(Predicate<? super RESULT> predicate,
String description)
Verifies that the
CompletableFuture is completed normally with a result matching the predicate,
the String parameter is used in the error message. |
SELF |
isDone()
Verifies that the
CompletableFuture is done i.e. |
SELF |
isNotCancelled()
Verifies that the
CompletableFuture is not cancelled. |
SELF |
isNotCompleted()
Verifies that the
CompletableFuture is not completed normally (i.e. |
SELF |
isNotCompletedExceptionally()
Verifies that the
CompletableFuture is not completed exceptionally. |
SELF |
isNotDone()
Verifies that the
CompletableFuture is not done. |
protected ObjectAssert<RESULT> |
newObjectAssert(RESULT objectUnderTest) |
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<?,?>> |
succeedsWithin(Duration timeout,
InstanceOfAssertFactory<RESULT,ASSERT> assertFactory)
Waits if necessary for at most the given time for this future to complete, the
InstanceOfAssertFactory
parameter is used to return assertions specific to the the future's result type. |
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<?,?>> |
succeedsWithin(long timeout,
TimeUnit unit,
InstanceOfAssertFactory<RESULT,ASSERT> assertFactory)
Waits if necessary for at most the given time for this future to complete, the
InstanceOfAssertFactory
parameter is used to return assertions specific to the the future's result type. |
areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, inBinary, inHexadecimal, is, isElementOfCustomAssert, 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, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, usingRecursiveComparison, usingRecursiveComparison, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnErrorclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitas, as, as, describedAsprotected AbstractCompletableFutureAssert(CompletableFuture<RESULT> actual, Class<?> selfType)
public SELF isDone()
CompletableFuture is done i.e. completed normally, exceptionally, or via cancellation.
Assertion will pass :
assertThat(CompletableFuture.completedFuture("something")).isDone();
Assertion will fail :
assertThat(new CompletableFuture()).isDone();CompletableFuture.isDone()public SELF isNotDone()
CompletableFuture is not done.
Assertion will pass :
assertThat(new CompletableFuture()).isNotDone();
Assertion will fail :
assertThat(CompletableFuture.completedFuture("something")).isNotDone();CompletableFuture.isDone()public SELF isCompletedExceptionally()
CompletableFuture is completed exceptionally.
Possible causes include cancellation, explicit invocation of completeExceptionally, and abrupt termination of a CompletionStage action.
Assertion will pass :
CompletableFuture future = new CompletableFuture();
future.completeExceptionally(new RuntimeException());
assertThat(future).isCompletedExceptionally();
Assertion will fail :
assertThat(CompletableFuture.completedFuture("something")).isCompletedExceptionally();CompletableFuture.isCompletedExceptionally()public SELF isNotCompletedExceptionally()
CompletableFuture is not completed exceptionally.
Assertion will pass :
assertThat(CompletableFuture.completedFuture("something")).isNotCompletedExceptionally();
Assertion will fail :
CompletableFuture future = new CompletableFuture();
future.completeExceptionally(new RuntimeException());
assertThat(future).isNotCompletedExceptionally();CompletableFuture.isCompletedExceptionally()public SELF isCancelled()
CompletableFuture is cancelled.
Assertion will pass :
CompletableFuture future = new CompletableFuture();
future.cancel(true);
assertThat(future).isCancelled();
Assertion will fail :
assertThat(new CompletableFuture()).isCancelled();CompletableFuture.isCancelled()public SELF isNotCancelled()
CompletableFuture is not cancelled.
Assertion will pass :
assertThat(new CompletableFuture()).isNotCancelled();
Assertion will fail :
CompletableFuture future = new CompletableFuture();
future.cancel(true);
assertThat(future).isNotCancelled();CompletableFuture.isCancelled()public SELF isCompleted()
CompletableFuture is completed normally (i.e.done but not
completed exceptionally) or cancelled.
Assertion will pass :
assertThat(CompletableFuture.completedFuture("something")).isCompleted();
Assertion will fail :
assertThat(new CompletableFuture()).isCompleted();public SELF isNotCompleted()
CompletableFuture is not completed normally (i.e. incomplete, failed or cancelled).
Assertion will pass :
assertThat(new CompletableFuture()).isNotCompleted();
Assertion will fail :
assertThat(CompletableFuture.completedFuture("something")).isNotCompleted();public SELF isCompletedWithValue(RESULT expected)
CompletableFuture is completed normally with the expected result.
Assertion will pass :
assertThat(CompletableFuture.completedFuture("something"))
.isCompletedWithValue("something");
Assertion will fail :
assertThat(CompletableFuture.completedFuture("something"))
.isCompletedWithValue("something else");expected - the expected result value of the CompletableFuture.public SELF isCompletedWithValueMatching(Predicate<? super RESULT> predicate)
CompletableFuture is completed normally with a result matching the predicate.
Assertion will pass :
assertThat(CompletableFuture.completedFuture("something"))
.isCompletedWithValueMatching(result -> result.equals("something"));
Assertion will fail :
assertThat(CompletableFuture.completedFuture("something"))
.isCompletedWithValueMatching(result -> result.equals("something else"));predicate - the Predicate to apply.public SELF isCompletedWithValueMatching(Predicate<? super RESULT> predicate, String description)
CompletableFuture is completed normally with a result matching the predicate,
the String parameter is used in the error message.
Assertion will pass :
assertThat(CompletableFuture.completedFuture("something"))
.isCompletedWithValueMatching(result -> result != null, "expected not null");
Assertion will fail :
assertThat(CompletableFuture.completedFuture("something"))
.isCompletedWithValueMatching(result -> result == null, "expected null");
Error message is:
Expecting:
<"something">
to match 'expected null' predicate.@Deprecated public SELF hasFailed()
Combine isCompletedExceptionally with isNotCancelled instead:
assertThat(future).isCompletedExceptionally()
.isNotCancelled();
This assertion is deprecated to change the semantics of failed to correspond to CompletableFuture.get() failing.
Original javadoc
Verifies that the CompletableFuture has completed exceptionally but has not been cancelled,
this assertion is equivalent to:
assertThat(future).isCompletedExceptionally()
.isNotCancelled();
Assertion will pass :
CompletableFuture future = new CompletableFuture();
future.completeExceptionally(new RuntimeException());
assertThat(future).hasFailed();
Assertion will fail :
CompletableFuture future = new CompletableFuture();
future.cancel(true);
assertThat(future).hasFailed();@Deprecated public SELF hasNotFailed()
Use matches with the following combination instead:
assertThat(future).matches (f -> f.isNotCompletedExceptionally() || f.isCancelled());
This assertion is deprecated because its semantic is not obvious.
Original javadoc
Verifies that the CompletableFuture has not failed i.e: incomplete, completed or cancelled.
This is different from isNotCompletedExceptionally() as a cancelled future has not failed but is completed exceptionally.
Assertion will pass :
CompletableFuture future = new CompletableFuture();
future.cancel(true);
assertThat(future).hasNotFailed();
Assertion will fail :
CompletableFuture future = new CompletableFuture();
future.completeExceptionally(new RuntimeException());
assertThat(future).hasNotFailed();public ObjectAssert<RESULT> succeedsWithin(Duration timeout)
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);timeout - the maximum time to waitAssertionError - if the actual CompletableFuture is null.AssertionError - if the actual CompletableFuture does not succeed within the given timeout.protected ObjectAssert<RESULT> newObjectAssert(RESULT objectUnderTest)
public ObjectAssert<RESULT> succeedsWithin(long timeout, TimeUnit unit)
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);timeout - the maximum time to waitunit - the time unit of the timeout argumentAssertionError - if the actual CompletableFuture is null.AssertionError - if the actual CompletableFuture does not succeed within the given timeout.public <ASSERT extends AbstractAssert<?,?>> ASSERT succeedsWithin(Duration timeout, InstanceOfAssertFactory<RESULT,ASSERT> assertFactory)
InstanceOfAssertFactory
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();ASSERT - the type of the resulting Asserttimeout - the maximum time to waitassertFactory - the factory which verifies the type and creates the new AssertAssert instance for assertions chaining on the value of the CompletableFutureAssertionError - if the actual CompletableFuture is null.IllegalStateException - if the actual CompletableFuture does not succeed within the given timeout.public <ASSERT extends AbstractAssert<?,?>> ASSERT succeedsWithin(long timeout, TimeUnit unit, InstanceOfAssertFactory<RESULT,ASSERT> assertFactory)
InstanceOfAssertFactory
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();ASSERT - the type of the resulting Asserttimeout - the maximum time to waitunit - the time unit of the timeout argumentassertFactory - the factory which verifies the type and creates the new AssertAssert instance for assertions chaining on the value of the CompletableFutureAssertionError - if the actual CompletableFuture is null.AssertionError - if the actual CompletableFuture does not succeed within the given timeout.@Deprecated public AbstractThrowableAssert<?,? extends Throwable> hasFailedWithThrowableThat()
Although not 100% the same, consider using failsWithin(Duration) or failsWithin(long, TimeUnit) instead:
CompletableFuture future = new CompletableFuture();
future.completeExceptionally(new RuntimeException("boom!"));
assertThat(future).failsWithin(1, TimeUnit.SECONDS)
.withThrowableOfType(RuntimeException.class)
.withMessage("boom!");
This assertion is deprecated because it relies on hasFailed() semantics which we want to move away from (they
are not clear!) and to use failure semantics corresponding to CompletableFuture.get() failing.
Original javadoc
Verifies that the CompletableFuture has completed exceptionally and
returns a Throwable assertion object allowing to check the Throwable that has caused the future to fail.
Assertion will pass :
CompletableFuture future = new CompletableFuture();
future.completeExceptionally(new RuntimeException("boom!"));
assertThat(future).hasFailedWithThrowableThat().isInstanceOf(RuntimeException.class);
.hasMessage("boom!");
Assertion will fail :
CompletableFuture future = new CompletableFuture();
future.completeExceptionally(new RuntimeException());
assertThat(future).hasFailedWithThrowableThat().isInstanceOf(IllegalArgumentException.class);
public WithThrowable failsWithin(Duration timeout)
Future.get(long, TimeUnit)) and returns
the exception that caused the failure for further (exception) assertions, the exception can be any of
InterruptedException, ExecutionException, TimeoutException or CancellationException.
WARNING
failsWithin does not fully integrate with soft assertions, if the future completes the test will fail immediately (the
error is not collected as a soft assertion error), if the assertion succeeds the chained assertions are executed and any
errors will be collected as a soft assertion errors.
The rationale is that if we collect failsWithin error as a soft assertion error, the chained assertions would be
executed but that does not make sense since there is no exception to check as the future has completed.
Examples:
CompletableFuture<?> future = futureCompletingAfterMs(100);
// assertion succeeds as the future is not completed after 50ms
assertThat(future).failsWithin(Duration.ofMillis(50))
.withThrowableOfType(TimeoutException.class)
.withMessage(null);
// fails as the future is completed after within 200ms
assertThat(future).failsWithin(Duration.ofMillis(200));timeout - the maximum time to waitAssertionError - if the actual CompletableFuture is null.AssertionError - if the actual CompletableFuture succeeds within the given timeout.public WithThrowable failsWithin(long timeout, TimeUnit unit)
Future.get(long, TimeUnit)) and returns
the exception that caused the failure for further (exception) assertions, the exception can be any of
InterruptedException, ExecutionException, TimeoutException or CancellationException.
WARNING
failsWithin does not fully integrate with soft assertions, if the future completes the test will fail immediately (the
error is not collected as a soft assertion error), if the assertion succeeds the chained assertions are executed and any
errors will be collected as a soft assertion errors.
The rationale is that if we collect failsWithin error as a soft assertion error, the chained assertions would be
executed but that does not make sense since there is no exception to check as the future has completed.
Examples:
CompletableFuture<?> future = futureCompletingAfterMs(100);
// assertion succeeds as the future is not completed after 50ms
assertThat(future).failsWithin(50, TimeUnit.MILLISECONDS)
.withThrowableOfType(TimeoutException.class)
.withMessage(null);
// fails as the future is completed after within 200ms
assertThat(future).failsWithin(200, TimeUnit.MILLISECONDS);timeout - the maximum time to waitunit - the time unitAssertionError - if the actual CompletableFuture is null.AssertionError - if the actual CompletableFuture succeeds within the given timeout.Copyright © 2025. All rights reserved.