Class AbstractCompletableFutureAssert<SELF extends AbstractCompletableFutureAssert<SELF,RESULT>,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
CompletableFuture.-
Field Summary
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, info, myself, objects, throwUnsupportedExceptionOnEquals -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractCompletableFutureAssert(CompletableFuture<RESULT> actual, Class<?> selfType) -
Method Summary
Modifier and TypeMethodDescriptioncompletesExceptionallyWithin(long timeout, TimeUnit unit) Checks that the future completes exceptionally within the given time (by callingFuture.get(long, TimeUnit)) and returns the exception that caused the failure for further (exception) assertions.completesExceptionallyWithin(Duration timeout) Checks that the future completes exceptionally within the given time (by callingFuture.get(long, TimeUnit)) and returns the exception that caused the failure for further (exception) assertions.failsWithin(long timeout, TimeUnit unit) Checks that the future does not complete within the given time (by callingFuture.get(long, TimeUnit)) and returns the exception that caused the failure for further (exception) assertions, the exception can be any ofInterruptedException,ExecutionException,TimeoutExceptionorCancellationException.failsWithin(Duration timeout) Checks that the future does not complete within the given time (by callingFuture.get(long, TimeUnit)) and returns the exception that caused the failure for further (exception) assertions, the exception can be any ofInterruptedException,ExecutionException,TimeoutExceptionorCancellationException.Deprecated.Combine isCompletedExceptionally with isNotCancelled instead:AbstractThrowableAssert<?, ? extends Throwable> Deprecated.Although not 100% the same, consider usingfailsWithin(Duration)orfailsWithin(long, TimeUnit)instead:Deprecated.Use matches with the following combination instead:Verifies that theCompletableFutureiscancelled.Verifies that theCompletableFutureis completed normally (i.e.donebut notcompleted exceptionally) orcancelled.Verifies that theCompletableFutureiscompleted exceptionally.isCompletedWithValue(RESULT expected) Verifies that theCompletableFutureis completed normally with theexpectedresult.isCompletedWithValueMatching(Predicate<? super RESULT> predicate) Verifies that theCompletableFutureis completed normally with a result matching thepredicate.isCompletedWithValueMatching(Predicate<? super RESULT> predicate, String description) Verifies that theCompletableFutureis completed normally with a result matching thepredicate, the String parameter is used in the error message.isCompletedWithValueMatchingWithin(Predicate<RESULT> resultPredicate, Duration completionDuration) Verifies that theCompletableFuturewill complete normally withing givenDurationwith a value that matches the givenPredicate.isDone()Verifies that theCompletableFutureisdonei.e.Verifies that theCompletableFutureis not cancelled.Verifies that theCompletableFutureis not completed normally (i.e.Verifies that theCompletableFutureis not completed exceptionally.Verifies that theCompletableFutureis not done.protected ObjectAssert<RESULT> newObjectAssert(RESULT objectUnderTest) 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, theInstanceOfAssertFactoryparameter is used to return assertions specific to the future's result type.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, theInstanceOfAssertFactoryparameter is used to return assertions specific to the future's result type.Methods inherited from class org.assertj.core.api.AbstractAssert
actual, areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, doesNotHaveToString, doesNotMatch, doesNotMatch, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, 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, usingEquals, usingEquals, usingRecursiveAssertion, usingRecursiveAssertion, usingRecursiveComparison, usingRecursiveComparison, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnErrorMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.assertj.core.api.Descriptable
as, as, as, describedAs, describedAs
-
Constructor Details
-
AbstractCompletableFutureAssert
-
-
Method Details
-
isDone
Verifies that theCompletableFutureisdonei.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:
-
isNotDone
Verifies that theCompletableFutureis not done.Assertion will pass :
Assertion will fail :assertThat(new CompletableFuture()).isNotDone();assertThat(CompletableFuture.completedFuture("something")).isNotDone();- Returns:
- this assertion object.
- See Also:
-
isCompletedExceptionally
Verifies that theCompletableFutureiscompleted exceptionally.Possible causes include cancellation, explicit invocation of completeExceptionally, and abrupt termination of a CompletionStage action.
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:
-
isNotCompletedExceptionally
Verifies that theCompletableFutureis 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:
-
isCancelled
Verifies that theCompletableFutureiscancelled.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:
-
isNotCancelled
Verifies that theCompletableFutureis 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:
-
isCompleted
Verifies that theCompletableFutureis completed normally (i.e.donebut notcompleted exceptionally) orcancelled.Assertion will pass :
Assertion will fail :assertThat(CompletableFuture.completedFuture("something")).isCompleted();assertThat(new CompletableFuture()).isCompleted();- Returns:
- this assertion object.
-
isNotCompleted
Verifies that theCompletableFutureis 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
Verifies that theCompletableFutureis completed normally with theexpectedresult.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.
-
isCompletedWithValueMatchingWithin
public SELF isCompletedWithValueMatchingWithin(Predicate<RESULT> resultPredicate, Duration completionDuration) Verifies that theCompletableFuturewill complete normally withing givenDurationwith a value that matches the givenPredicate.Assertion succeeds:
assertThat(completedFuture("something")).isCompletedWithValueMatchingWithin(result -> result.equals("something"), Duration.ofSeconds(10));Assertions fail:
assertThat(failedFuture("something")).isCompletedWithValueMatchingWithin(result -> result.equals("something"), Duration.ofSeconds(10)); Future futureCompletedAfterFiveSeconds = ... ; assertThat(futureCompletedAfterFiveSeconds).isCompletedWithValueMatchingWithin(result -> result.equals("something"), Duration.ofSeconds(10));- Parameters:
resultPredicate- thePredicateto apply.completionDuration- the maximum time to wait- Returns:
- a new assertion object on the future's result.
- Throws:
AssertionError- if the actualCompletableFutureisnull.AssertionError- if the actualCompletableFuturedoes not succeed within the given timeout with the satisfying value.
-
isCompletedWithValueMatching
Verifies that theCompletableFutureis 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- thePredicateto apply.- Returns:
- this assertion object.
-
isCompletedWithValueMatching
Verifies that theCompletableFutureis 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. -
hasFailed
Deprecated.Combine isCompletedExceptionally with isNotCancelled instead:
This assertion is deprecated to change the semantics of failed to correspond toassertThat(future).isCompletedExceptionally() .isNotCancelled();CompletableFuture.get()failing.Original javadoc
Verifies that the
CompletableFuturehas 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
Deprecated.Use matches with the following combination instead:
This assertion is deprecated because its semantic is not obvious.assertThat(future).matches (f -> f.isNotCompletedExceptionally() || f.isCancelled());Original javadoc
Verifies that the
CompletableFuturehas 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
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 future's result.
- Throws:
AssertionError- if the actualCompletableFutureisnull.AssertionError- if the actualCompletableFuturedoes not succeed within the given timeout.
-
newObjectAssert
-
succeedsWithin
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 future's result.
- Throws:
AssertionError- if the actualCompletableFutureisnull.AssertionError- if the actualCompletableFuturedoes not succeed within the given timeout.
-
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, theInstanceOfAssertFactoryparameter is used to return assertions specific to 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
Assertinstance for assertions chaining on the value of theCompletableFuture - Throws:
AssertionError- if the actualCompletableFutureisnull.IllegalStateException- if the actualCompletableFuturedoes 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, theInstanceOfAssertFactoryparameter is used to return assertions specific to 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
Assertinstance for assertions chaining on the value of theCompletableFuture - Throws:
AssertionError- if the actualCompletableFutureisnull.AssertionError- if the actualCompletableFuturedoes not succeed within the given timeout.
-
hasFailedWithThrowableThat
Deprecated.Although not 100% the same, consider using
failsWithin(Duration)orfailsWithin(long, TimeUnit)instead:
This assertion is deprecated because it relies onCompletableFuture future = new CompletableFuture(); future.completeExceptionally(new RuntimeException("boom!")); assertThat(future).failsWithin(1, TimeUnit.SECONDS) .withThrowableOfType(RuntimeException.class) .withMessage("boom!");hasFailed()semantics which we want to move away from (they are not clear!) and to use failure semantics corresponding toCompletableFuture.get()failing.Original javadoc
Verifies that the
CompletableFuturehas 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.
-
failsWithin
Checks that the future does not complete within the given time (by callingFuture.get(long, TimeUnit)) and returns the exception that caused the failure for further (exception) assertions, the exception can be any ofInterruptedException,ExecutionException,TimeoutExceptionorCancellationException.WARNING
failsWithindoes 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 collectfailsWithinerror 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));- Parameters:
timeout- the maximum time to wait- Returns:
- a new assertion instance on the future's exception.
- Throws:
AssertionError- if the actualCompletableFutureisnull.AssertionError- if the actualCompletableFuturesucceeds within the given timeout.- Since:
- 3.18.0
-
failsWithin
Checks that the future does not complete within the given time (by callingFuture.get(long, TimeUnit)) and returns the exception that caused the failure for further (exception) assertions, the exception can be any ofInterruptedException,ExecutionException,TimeoutExceptionorCancellationException.WARNING
failsWithindoes 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 collectfailsWithinerror 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);- Parameters:
timeout- the maximum time to waitunit- the time unit- Returns:
- a new assertion instance on the future's exception.
- Throws:
AssertionError- if the actualCompletableFutureisnull.AssertionError- if the actualCompletableFuturesucceeds within the given timeout.- Since:
- 3.18.0
-
completesExceptionallyWithin
Checks that the future completes exceptionally within the given time (by callingFuture.get(long, TimeUnit)) and returns the exception that caused the failure for further (exception) assertions. The exception can beExecutionExceptionorCancellationException.Examples:
CompletableFuture<Void> completableFuture = futureThrowingExceptionAfterMs(100); // assertion fails as the future is not completed (neither successfully nor exceptionally) within 1 ms assertThat(completableFuture).completesExceptionallyWithin(1, MILLISECONDS); // assertion succeeds as after a longer wait the future is completed with the expected exception assertThat(completableFuture).completesExceptionallyWithin(200, MILLISECONDS) .withThrowableOfType(ExecutionException.class) .withMessageContaining("boom!");- Parameters:
timeout- the maximum duration to wait- Returns:
- a new assertion instance on the future's exception.
- Throws:
AssertionError- if the actualCompletableFutureisnull.AssertionError- if the actualCompletableFuturesucceeds or doesn't complete at all within the given timeout.- Since:
- 3.27.0
-
completesExceptionallyWithin
Checks that the future completes exceptionally within the given time (by callingFuture.get(long, TimeUnit)) and returns the exception that caused the failure for further (exception) assertions. The exception can beExecutionExceptionorCancellationException.Examples:
CompletableFuture<Void> completableFuture = futureThrowingExceptionAfterMs(100); // assertion fails as the future is not completed (neither successfully nor exceptionally) within 1 ms assertThat(completableFuture).completesExceptionallyWithin(Duration.ofMillis(1)); // assertion succeeds as after a longer wait the future is completed with the expected exception assertThat(completableFuture).completesExceptionallyWithin(Duration.ofMillis(200)) .withThrowableOfType(ExecutionException.class) .withMessageContaining("boom!");- Parameters:
timeout- the maximum time to waitunit- the timeout unit- Returns:
- a new assertion instance on the future's exception.
- Throws:
AssertionError- if the actualCompletableFutureisnull.AssertionError- if the actualCompletableFuturesucceeds or doesn't complete at all within the given timeout.- Since:
- 3.27.0
-