Class TestTime


  • public final class TestTime
    extends Time
    TestTime is a singleton class that provides access to the current time and allows to manipulate it both in production and test code. It is an extension of Time. TestTime is designed to be used in tests only.
    • Method Detail

      • testInstance

        public static TestTime testInstance()
        Returns the singleton instance of TestTime.
        Returns:
        the singleton instance of TestTime
      • now

        public Instant now()
        Returns the current instant using the clock.
        Overrides:
        now in class Time
        Returns:
        the current instant
      • setClock

        public void setClock​(Clock clock)
        Replace the clock used by Time and TestTime with a custom one.
            // Prepare a fixed clock
            ZoneId zoneId = TimeZone.getTimeZone("Europe/Warsaw").toZoneId();
            ZonedDateTime dateTime = LocalDateTime.of(2001, 11, 23, 12, 15).atZone(zoneId);
            Clock fixedClock = Clock.fixed(dateTime.toInstant(), zoneId);
        
            // Set the fixed clock as the clock used by Time in the production code
            TestTime.testInstance().setClock(fixedClock);
         
        Overrides:
        setClock in class Time
        Parameters:
        clock -
      • fastForward

        public void fastForward​(Duration duration)
        Jump the clock forward by the given duration.
        Parameters:
        duration - the duration to jump forward
      • fastBackward

        public void fastBackward​(Duration duration)
        Jump the clock backward by the given duration.
        Parameters:
        duration - the duration to jump backward
      • timeFlow

        public void timeFlow​(Duration step,
                             Instant endTime,
                             int flowSpeedMillis)
        Simulate time flow by jumping the clock forward by the given step every flowSpeedMillis. This will replace the clock used by Time in the production code.
            // Prepare a fixed clock
            ZoneId zoneId = TimeZone.getTimeZone("Europe/Warsaw").toZoneId();
            ZonedDateTime dateTime = LocalDateTime.of(2001, 11, 23, 12, 15).atZone(zoneId);
            Clock fixedClock = Clock.fixed(dateTime.toInstant(), zoneId);
        
            // Set the fixed clock as the clock used by Time in the production code
            TestTime.testInstance().setClock(fixedClock);
        
            // Prepare time flow parameters
            Duration step = Duration.of(1, ChronoUnit.MINUTES);
            Instant endTime = fixedClock.instant().plus(10, ChronoUnit.MINUTES);
            int flowSpeedMillis = 100;
        
            // Simulate time flow
            TestTime.testInstance().timeFlow(step, endTime, flowSpeedMillis);
         
        Parameters:
        step - the duration to jump forward
        endTime - the time when the simulated time flow should stop
        flowSpeedMillis - the speed of the simulated time flow in milliseconds
        Throws:
        IllegalArgumentException - if flowSpeedMillis is not positive or endTime is before the current time
        RuntimeException - if interrupted while sleeping between jumps
      • clearObservers

        public void clearObservers()
        Remove all registered observers.