Class AbstractFutureAssert<SELF extends AbstractFutureAssert<SELF,ACTUAL,RESULT>,ACTUAL extends Future<RESULT>,RESULT>
- All Implemented Interfaces:
Assert<SELF,,ACTUAL> Descriptable<SELF>,ExtensionPoints<SELF,ACTUAL>
- Direct Known Subclasses:
FutureAssert
-
Field Summary
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, info, myself, objects, throwUnsupportedExceptionOnEquals -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfailsWithin(long timeout, TimeUnit unit) Checks that the future does not complete within the given time and returns the exception that caused the failure for further (exception) assertions, the exception can be any ofInterruptedException,ExecutionException,TimeoutExceptionorCancellationExceptionas perFuture.get(long, TimeUnit).failsWithin(Duration timeout) Checks that the future does not complete within the given time and returns the exception that caused the failure for further (exception) assertions, the exception can be any ofInterruptedException,ExecutionException,TimeoutExceptionorCancellationExceptionas perFuture.get(long, TimeUnit).Verifies that theFutureis cancelled.isDone()Verifies that theFutureis done.Verifies that theFutureis not cancelled.Verifies that theFutureis not done.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
-
AbstractFutureAssert
-
-
Method Details
-
isCancelled
Verifies that theFutureis cancelled.Example:
ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(new Callable<String>() { @Override public String call() throws Exception { return "done"; } }); // assertion will fail: assertThat(future).isCancelled(); // assertion will pass: future.cancel(true); assertThat(future).isCancelled();- Returns:
- this assertion object.
- Since:
- 2.7.0 / 3.7.0
- See Also:
-
isNotCancelled
Verifies that theFutureis not cancelled.Example:
ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(new Callable<String>() { @Override public String call() throws Exception { return "done"; } }); // assertion will pass: assertThat(future).isNotCancelled(); // assertion will fail: future.cancel(true); assertThat(future).isNotCancelled();- Returns:
- this assertion object.
- Since:
- 2.7.0 / 3.7.0
- See Also:
-
isDone
Verifies that theFutureis done.Example:
ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(new Callable<String>() { @Override public String call() throws Exception { return "done"; } }); // assertion will pass: assertThat(future).isDone(); future = executorService.submit(new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(1000); return "done"; } }); // assertion will fail: assertThat(future).isDone();- Returns:
- this assertion object.
- Since:
- 2.7.0 / 3.7.0
- See Also:
-
isNotDone
Verifies that theFutureis not done.Example:
ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(1000); return "done"; } }); // assertion will pass: assertThat(future).isNotDone(); future = executorService.submit(new Callable<String>() { @Override public String call() throws Exception { return "done"; } }); // assertion will fail: assertThat(future).isNotDone();- Returns:
- this assertion object.
- Since:
- 2.7.0 / 3.7.0
- See Also:
-
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.
WARNING
succeedsWithindoes not fully integrate with soft assertions, if it fails the test will fail immediately (the error is not collected as a soft assertion error), if it succeeds the chained assertions are executed and any error will be collected as a soft assertion error.
The rationale is that if we collectedsucceedsWithinerror as a soft assertion error, the chained assertions would be executed against a future value that is actually not available.To get assertions for the future result's type use
succeedsWithin(Duration, InstanceOfAssertFactory)instead.Examples:
ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(() -> { Thread.sleep(100); return "ook!"; }); Duration timeout = Duration.ofMillis(200); // assertion succeeds assertThat(future).succeedsWithin(timeout) .isEqualTo("ook!"); // fails as the future is not done after the given timeout assertThat(future).succeedsWithin(Duration.ofMillis(50)); // fails as the future is cancelled Future<String> future = ... ; 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.- Since:
- 3.17.0
-
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.
WARNING
succeedsWithindoes not fully integrate with soft assertions, if it fails the test will fail immediately (the error is not collected as a soft assertion error), if it succeeds the chained assertions are executed and any error will be collected as a soft assertion error.
The rationale is that if we collectedsucceedsWithinerror as a soft assertion error, the chained assertions would be executed against a future value that is actually not available.To get assertions for the future result's type use
succeedsWithin(long, TimeUnit, InstanceOfAssertFactory)instead.Examples:
ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(() -> { Thread.sleep(100); return "ook!"; }); // assertion succeeds assertThat(future).succeedsWithin(200, TimeUnit.MILLISECONDS) .isEqualTo("ook!"); // fails as the future is not done after the given timeout assertThat(future).succeedsWithin(50, TimeUnit.MILLISECONDS); // fails as the future is cancelled Future<String> future = ... ; future.cancel(false); assertThat(future).succeedsWithin(200, 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 actualFutureisnull.AssertionError- if the actualFuturedoes not succeed within the given timeout.- Since:
- 3.17.0
-
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.
WARNING
succeedsWithindoes not fully integrate with soft assertions, if it fails the test will fail immediately (the error is not collected as a soft assertion error), if it succeeds the chained assertions are executed and any error will be collected as a soft assertion error.
The rationale is that if we collectedsucceedsWithinerror as a soft assertion error, the chained assertions would be executed against a future value that is actually not available.Examples:
ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(() -> { Thread.sleep(100); return "ook!"; }); Duration timeout = Duration.ofMillis(200); // assertion succeeds, contains(String...) assertion can be called because InstanceOfAssertFactories.STRING // indicates AssertJ to allow String assertions after succeedsWithin. assertThat(future).succeedsWithin(timeout, InstanceOfAssertFactories.STRING) .contains("ok"); // fails as the future is not done after the given timeout // as() is syntactic sugar for better readability. assertThat(future).succeedsWithin(Duration.ofMillis(50), as(STRING)); // 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 theFuture - Throws:
AssertionError- if the actualFutureisnull.IllegalStateException- if the actualFuturedoes not succeed within the given timeout.- Since:
- 3.17.0
-
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.
WARNING
succeedsWithindoes not fully integrate with soft assertions, if it fails the test will fail immediately (the error is not collected as a soft assertion error), if it succeeds the chained assertions are executed and any error will be collected as a soft assertion error.
The rationale is that if we collectedsucceedsWithinerror as a soft assertion error, the chained assertions would be executed against a future value that is actually not available.Examples:
ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(() -> { Thread.sleep(100); return "ook!"; }); // assertion succeeds, contains(String...) assertion can be called because InstanceOfAssertFactories.STRING // indicates AssertJ to allow String assertions after succeedsWithin. assertThat(future).succeedsWithin(200, TimeUnit.MILLISECONDS, InstanceOfAssertFactories.STRING) .contains("ok"); // fails as the future is not done after the given timeout // as() is syntactic sugar for better readability. assertThat(future).succeedsWithin(50, TimeUnit.MILLISECONDS, as(STRING)); // assertion fails if the narrowed type for assertions is incompatible with the future's result type. assertThat(future).succeedsWithin(200, 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 theFuture - Throws:
AssertionError- if the actualFutureisnull.AssertionError- if the actualFuturedoes not succeed within the given timeout.- Since:
- 3.17.0
-
failsWithin
Checks that the future does not complete within the given time and returns the exception that caused the failure for further (exception) assertions, the exception can be any ofInterruptedException,ExecutionException,TimeoutExceptionorCancellationExceptionas perFuture.get(long, TimeUnit).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:
ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(() -> { Thread.sleep(100); return "ook!"; }); // 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 and returns the exception that caused the failure for further (exception) assertions, the exception can be any ofInterruptedException,ExecutionException,TimeoutExceptionorCancellationExceptionas perFuture.get(long, TimeUnit).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:
ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(() -> { Thread.sleep(100); return "ook!"; }); // 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 the given timeout duration 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
-