Class EventualCondition


  • public class EventualCondition
    extends java.lang.Object
    A test condition that is evaluated repeatedly during a reasonable period of time. It's intended to assert asynchronous changes. Default evaluation period is 10 seconds and the condition is evaluated approximately every 500 milliseconds.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static EventualCondition eventualCondition()
      Creates evaluation condition with default evaluation period and interval.
      void expect​(java.lang.Runnable body)
      Evaluates the conditions represented by the code body.
      void retains​(java.lang.Runnable body)
      Evaluates whether the code body does not throw exceptions within the duration used by this condition.
      EventualCondition within​(java.time.Duration duration)
      Specifies duration period for the following eventual condition.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • eventualCondition

        public static EventualCondition eventualCondition()
        Creates evaluation condition with default evaluation period and interval.
        Returns:
        a condition that is evaluated about every 500 milliseconds within 10 seconds period.
      • within

        public EventualCondition within​(java.time.Duration duration)
        Specifies duration period for the following eventual condition.
        Parameters:
        duration - duration to applie for the eventual conditions. Default value, if not specified, is 10 seconds.
        Returns:
        an EventualCondition with the duration specified.
      • expect

        public void expect​(java.lang.Runnable body)
        Evaluates the conditions represented by the code body. For example, let's say we have a class Job that changes its state asynchronously. In this case this method can be used like this:
               Job job = new Job
        
               // start the job
               job.start();
        
               // assert the asynchronous condition that the job eventually finishes.
               eventualCondition().expect(() -> assertEquals("finished", job.getState());
         
        In other words, this method waits withing the time period used by this condition until the body stops throwing exceptions. As soon, as the body did not throw an exception on its subsequent execution, the method returns. If the time period has been exceeded and the body still keeps throwing the exception, that exception will be propagated.
        Parameters:
        body - a code to evaluate. It should throw an exception when the condition is not met.
      • retains

        public void retains​(java.lang.Runnable body)
        Evaluates whether the code body does not throw exceptions within the duration used by this condition. For example, let's say we have a class Job that changes its state asynchronously and we want to verify that it remains in the RUNNING state for a certain period of time. In this case this method can be used like this:
               Job job = new Job
        
               // start the job
               job.start();
        
               // assert the asynchronous condition that the job eventually finishes.
               eventualCondition().within(Duration.ofSeconds(3)).retains(() -> assertEquals("running", job.getState());
         
        If we use expect(Runnable) method here, then the condition will pass on first evaluation and we immediately leave the method without waiting to check that the condition still passes at the end of the desired time frame.
        Parameters:
        body - a code to evaluate. It should throw an exception when the condition is not met.