Top Fields Constructors Methods
org.assertj.core.api

public abstract Class AbstractFutureAssert<SELF extends AbstractFutureAssert<SELF, ACTUAL, RESULT>, ACTUAL extends Future<RESULT>, RESULT>

extends AbstractAssert<SELF extends AbstractFutureAssert<SELF, ACTUAL, RESULT>, ACTUAL extends Future<RESULT>>
Class Inheritance
Known Direct Subclasses
org.assertj.core.api.FutureAssert
Static Imports
org.assertj.core.api.AssertionsForClassTypes.assertThat

Field Summary

Modifier and TypeField and Description
pack-priv Futures
Inherited from org.assertj.core.api.AbstractAssert:
actualassertionErrorCreatorconditionscustomRepresentationinfomyselfobjectsprintAssertionsDescriptionthrowUnsupportedExceptionOnEquals

Constructor Summary

AccessConstructor and Description
protected
AbstractFutureAssert(ACTUAL actual, Class<?> selfType)

Method Summary

Modifier and TypeMethod and Description
public WithThrowable

Returns:

a new assertion instance on the future's exception.
failsWithin
(Duration
the maximum time to wait
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 of InterruptedException, ExecutionException, TimeoutException or CancellationException as per Future#get(long, TimeUnit).

public WithThrowable

Returns:

a new assertion instance on the future's exception.
failsWithin
(long
the maximum time to wait
timeout
,
TimeUnit
the time unit
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 of InterruptedException, ExecutionException, TimeoutException or CancellationException as per Future#get(long, TimeUnit).

private WithThrowable
private WithThrowable
internalFailsWithin(long timeout, TimeUnit unit)

private ObjectAssert<RESULT>
private ObjectAssert<RESULT>
internalSucceedsWithin(long timeout, TimeUnit unit)

public SELF

Returns:

this assertion object.
isCancelled
()

Verifies that the Future is cancelled.

public SELF

Returns:

this assertion object.
isDone
()

Verifies that the Future is done.

public SELF

Returns:

this assertion object.
isNotCancelled
()

Verifies that the Future is not cancelled.

public SELF

Returns:

this assertion object.
isNotDone
()

Verifies that the Future is not done.

public ObjectAssert<RESULT>

Returns:

a new assertion object on the future's result.
succeedsWithin
(Duration
the maximum time to wait
timeout
)

Waits if necessary for at most the given time for this future to complete and then returns its result for further assertions.

public ObjectAssert<RESULT>

Returns:

a new assertion object on the future's result.
succeedsWithin
(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Waits if necessary for at most the given time for this future to complete and then returns its result for further assertions.

public <
the type of the resulting Assert
ASSERT extends AbstractAssert<?, ?>
>
ASSERT

Returns:

a new narrowed Assert instance for assertions chaining on the value of the Future
succeedsWithin
(Duration
the maximum time to wait
timeout
,
InstanceOfAssertFactory<RESULT, ASSERT>
the factory which verifies the type and creates the new 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 future's result type.

public <
the type of the resulting Assert
ASSERT extends AbstractAssert<?, ?>
>
ASSERT

Returns:

a new narrowed Assert instance for assertions chaining on the value of the Future
succeedsWithin
(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
,
InstanceOfAssertFactory<RESULT, ASSERT>
the factory which verifies the type and creates the new 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 future's result type.

Inherited from org.assertj.core.api.AbstractAssert:
areEqualasInstanceOfasListassertionErrorasStringdescribedAsdescriptionTextdoesNotHavedoesNotHaveSameClassAsdoesNotHaveSameHashCodeAsdoesNotHaveToStringdoesNotHaveToStringequalsextractingextractingfailurefailureWithActualExpectedfailWithActualExpectedAndMessagefailWithMessagegetWritableAssertionInfohashashCodehasSameClassAshasSameHashCodeAshasToStringhasToStringinBinaryinHexadecimalisisElementOfCustomAssertisEqualToisExactlyInstanceOfisInisInisInstanceOfisInstanceOfAnyisInstanceOfSatisfyingisNotisNotEqualToisNotExactlyInstanceOfisNotInisNotInisNotInstanceOfisNotInstanceOfAnyisNotNullisNotOfAnyClassInisNotSameAsisNullisOfAnyClassInisSameAsmatchesmatchesnewListAssertInstanceoverridingErrorMessageoverridingErrorMessagesatisfiessatisfiessatisfiessatisfiesAnyOfsatisfiesAnyOfsatisfiesAnyOfForProxysatisfiesForProxysetCustomRepresentationsetDescriptionConsumersetPrintAssertionsDescriptionthrowAssertionErrorusingComparatorusingComparatorusingDefaultComparatorusingRecursiveAssertionusingRecursiveAssertionusingRecursiveComparisonusingRecursiveComparisonwithAssertionStatewithFailMessagewithFailMessagewithRepresentationwithThreadDumpOnError

Field Detail

futuresback to summary
pack-priv Futures futures
Annotations
@VisibleForTesting

Constructor Detail

AbstractFutureAssertback to summary
protected AbstractFutureAssert(ACTUAL actual, Class<?> selfType)

Method Detail

failsWithinback to summary
public WithThrowable 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 of InterruptedException, ExecutionException, TimeoutException or CancellationException as per Future#get(long, TimeUnit).

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:

 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:Duration

the maximum time to wait

Returns:WithThrowable

a new assertion instance on the future's exception.

Exceptions
AssertionError:
  • if the actual CompletableFuture is null.
  • if the actual CompletableFuture succeeds within the given timeout.
Since
3.18.0
failsWithinback to summary
public WithThrowable failsWithin(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 of InterruptedException, ExecutionException, TimeoutException or CancellationException as per Future#get(long, TimeUnit).

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:

 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:long

the maximum time to wait

unit:TimeUnit

the time unit

Returns:WithThrowable

a new assertion instance on the future's exception.

Exceptions
AssertionError:
  • if the actual CompletableFuture is null.
  • if the actual CompletableFuture succeeds within the given timeout.
Since
3.18.0
internalFailsWithinback to summary
private WithThrowable internalFailsWithin(Duration timeout)
internalFailsWithinback to summary
private WithThrowable internalFailsWithin(long timeout, TimeUnit unit)
internalSucceedsWithinback to summary
private ObjectAssert<RESULT> internalSucceedsWithin(Duration timeout)
internalSucceedsWithinback to summary
private ObjectAssert<RESULT> internalSucceedsWithin(long timeout, TimeUnit unit)
isCancelledback to summary
public SELF isCancelled()

Verifies that the Future is 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:SELF

this assertion object.

Since
2.7.0 / 3.7.0
See Also
Future#isCancelled()
isDoneback to summary
public SELF isDone()

Verifies that the Future is 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:SELF

this assertion object.

Since
2.7.0 / 3.7.0
See Also
Future#isDone()
isNotCancelledback to summary
public SELF isNotCancelled()

Verifies that the Future is 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:SELF

this assertion object.

Since
2.7.0 / 3.7.0
See Also
Future#isCancelled()
isNotDoneback to summary
public SELF isNotDone()

Verifies that the Future is 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:SELF

this assertion object.

Since
2.7.0 / 3.7.0
See Also
Future#isDone()
succeedsWithinback to summary
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.

WARNING

succeedsWithin does 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 collected succeedsWithin error 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:Duration

the maximum time to wait

Returns:ObjectAssert<RESULT>

a new assertion object on the future's result.

Exceptions
AssertionError:
  • if the actual CompletableFuture is null.
  • if the actual CompletableFuture does not succeed within the given timeout.
Since
3.17.0
succeedsWithinback to summary
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.

WARNING

succeedsWithin does 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 collected succeedsWithin error 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:long

the maximum time to wait

unit:TimeUnit

the time unit of the timeout argument

Returns:ObjectAssert<RESULT>

a new assertion object on the future's result.

Exceptions
AssertionError:
  • if the actual Future is null.
  • if the actual Future does not succeed within the given timeout.
Since
3.17.0
succeedsWithinback to summary
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, the InstanceOfAssertFactory parameter 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

succeedsWithin does 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 collected succeedsWithin error 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();
Parameters
<ASSERT>
the type of the resulting Assert
timeout:Duration

the maximum time to wait

assertFactory:InstanceOfAssertFactory<RESULT, ASSERT>

the factory which verifies the type and creates the new Assert

Returns:ASSERT

a new narrowed Assert instance for assertions chaining on the value of the Future

Exceptions
AssertionError:
if the actual Future is null.
IllegalStateException:
if the actual Future does not succeed within the given timeout.
Since
3.17.0
succeedsWithinback to summary
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, the InstanceOfAssertFactory parameter 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

succeedsWithin does 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 collected succeedsWithin error 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();
Parameters
<ASSERT>
the type of the resulting Assert
timeout:long

the maximum time to wait

unit:TimeUnit

the time unit of the timeout argument

assertFactory:InstanceOfAssertFactory<RESULT, ASSERT>

the factory which verifies the type and creates the new Assert

Returns:ASSERT

a new narrowed Assert instance for assertions chaining on the value of the Future

Exceptions
AssertionError:
  • if the actual Future is null.
  • if the actual Future does not succeed within the given timeout.
Since
3.17.0