Skip to content

Framework that allows to control time flow in an application and tests (something similar to java.time.Clock)

License

Notifications You must be signed in to change notification settings

mazurkin/chronometer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Framework similar to java.time.Clock that allows to control time flow in an application and tests.

public class TimeDependent {
    
    private final Chronometer chronometer;
    
    public TimeDependent(Chronometer chronometer) {
        this.chronometer = chronometer;
    }
    
    public void knock() {
        Instant now = chronometer.getInstant();  
        System.out.println("Current time: " + now);
    }
}

On production:

public class Main {
    
    public static void main(String[] args) throws Exception {
        TimeDependent d = new TimeDependent(SystemChronometer.INSTANCE);
        d.knock();
    }
}

In test:

public class Test {
    
    private MockChronometer chronometer;
    
    private TimeDependent d;
    
    @Before
    public void setUp() throws Exception {
        chronometer = MockChronometer.createFrozen("2017-04-21 14:22:12.000 Europe/Moscow", System.nanoTime());
        d = new TimeDependent(chronometer);
    }
    
    @Test
    public void test() {
        d.knock();
        chronometer.shiftBy(+1, TimeUnit.HOURS);
        d.knock();
        chronometer.shiftTo("2017-04-26 12:33:42.000 UTC");
        d.knock();
        chronometer.setMode(Mode.SYSTEM);
        d.knock();
    }
}

About

Framework that allows to control time flow in an application and tests (something similar to java.time.Clock)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages