https://github.com/state-machine-systems/mock-clock
A mock Clock implementation for Java 8
https://github.com/state-machine-systems/mock-clock
Last synced: 6 months ago
JSON representation
A mock Clock implementation for Java 8
- Host: GitHub
- URL: https://github.com/state-machine-systems/mock-clock
- Owner: state-machine-systems
- Created: 2015-12-10T15:15:00.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T00:12:43.000Z (over 5 years ago)
- Last Synced: 2024-10-31T23:02:47.677Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 13.7 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Java 8's `Clock` class is a big step in the right direction for testability, but it's still a pain. You can
test using `Clock.fixed()` but that's immutable so you can't test how your code responds to the passage of time.
You can use a mocking library to stub the `instant()` and `getZone()` methods, but that's still awkward and indirect.
This library provides a mutable `MockClock` class that can be constructed as of a given date and time, then adjusted as
needed. Here's an example of how to use it:
import com.statemachinesystems.mockclock.MockClock;
import java.time.ZoneId;
MockClock clock = MockClock.at(2015, 12, 10, 11, 16, ZoneId.of("UTC"));
ClassUnderTest testSubject = new ClassUnderTest(clock);
assertThat(testSubject.someMethod(), is(expectedValueAtStartTime));
clock.advanceBySeconds(30);
assertThat(testSubject.someMethod(), is(expectedValueAfter30Seconds));
This library is in the Maven Central repo, so just add the following chunk to your pom.xml (or the equivalent for Gradle/SBT/whatever):
com.statemachinesystems
mock-clock
1.0
© 2015 State Machine Systems Ltd. [Apache Licence, Version 2.0]( http://www.apache.org/licenses/LICENSE-2.0)