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

    public abstract class AbstractFutureAssert<SELF extends AbstractFutureAssert<SELF,​ACTUAL,​RESULT>,​ACTUAL extends Future<RESULT>,​RESULT>
    extends AbstractAssert<SELF,​ACTUAL>
    • Constructor Detail

      • AbstractFutureAssert

        protected AbstractFutureAssert​(ACTUAL actual,
                                       Class<?> selfType)
    • Method Detail

      • isCancelled

        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:
        this assertion object.
        Since:
        2.7.0 / 3.7.0
        See Also:
        Future.isCancelled()
      • isNotCancelled

        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:
        this assertion object.
        Since:
        2.7.0 / 3.7.0
        See Also:
        Future.isCancelled()
      • isDone

        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:
        this assertion object.
        Since:
        2.7.0 / 3.7.0
        See Also:
        Future.isDone()
      • isNotDone

        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:
        this assertion object.
        Since:
        2.7.0 / 3.7.0
        See Also:
        Future.isDone()
      • 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:

         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 the future's result.
        Throws:
        AssertionError - if the actual CompletableFuture is null.
        AssertionError - if the actual CompletableFuture does not succeed within the given timeout.
        Since:
        3.17.0
      • 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:

         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 wait
        unit - the time unit of the timeout argument
        Returns:
        a new assertion object on the the future's result.
        Throws:
        AssertionError - if the actual Future is null.
        AssertionError - if the actual Future does 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, the 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:

         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 resulting Assert
        Parameters:
        timeout - the maximum time to wait
        assertFactory - the factory which verifies the type and creates the new Assert
        Returns:
        a new narrowed Assert instance for assertions chaining on the value of the Future
        Throws:
        AssertionError - if the actual Future is null.
        IllegalStateException - if the actual Future does 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, the 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:

         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 resulting Assert
        Parameters:
        timeout - the maximum time to wait
        unit - the time unit of the timeout argument
        assertFactory - the factory which verifies the type and creates the new Assert
        Returns:
        a new narrowed Assert instance for assertions chaining on the value of the Future
        Throws:
        AssertionError - if the actual Future is null.
        AssertionError - if the actual Future does not succeed within the given timeout.
        Since:
        3.17.0