in Maven
<dependency>
<groupId>com.github.perfectacle</groupId>
<artifactId>time-machine</artifactId>
<version>1.0.0</version>
</dependency>
in Gradle
implementation 'com.github.perfectacle:time-machine:1.0.0'
Suppose you have a code that uses the now() method in the Date/Time API of Java 8.
public class SomeClass {
public static boolean someMethod() {
return someMethod2();
}
private static boolean someMethod2() {
return LocalTime.now().isAfter(LocalTime.of(12, 0, 0));
}
}
Time is out of control.
public class SomeClassTest {
// In only PM, this test passed
@Test
void someMethod() {
assertTrue(SomeClass.someMethod());
}
}
Therefore, meaningless parameters(current time of now) must be passed continuously.
public class SomeClass {
public static boolean someMethod(final LocalTime now) {
return someMethod2(now);
}
private static boolean someMethod2(final LocalTime now) {
return now.isAfter(LocalTime.of(12, 0, 0));
}
}
We can control time only by repeating this meaningless act(passing the parameters).
public class SomeClassTest {
// This test passed in always by passing parameter(current time of now)
@Test
void someMethod() {
assertTrue(SomeClass.someMethod(LocalTime.of(13, 0, 0)));
}
}
If the public method internally calls dozens of private methods, would you pass the current time to the parameter each time?
To eliminate this inefficiency, you should use Time Machine.
public class SomeClass {
public static boolean someMethod() {
return someMethod2();
}
private static boolean someMethod2() {
return TimeMachine.nowOfLocalTime().isAfter(LocalTime.of(12, 0, 0));
}
}
public class SomeClassTest {
// This test passed in always
@Test
void someMethod() {
TimeMachine.travelAt(LocalTime.of(13, 0, 0));
assertTrue(SomeClass.someMethod());
}
}
public class SomeClassTest {
@Test
void whenNotTraveled() {
// Reset to time machine to avoid being affected by other tests.
TimeMachine.reset();
final Instant nowOfInstant = Instant.now();
final Instant nowOfInstantWithTimeMachine = TimeMachine.nowOfInstant();
assertEquals(nowOfInstant.minusNanos(nowOfInstant.getNano()), nowOfInstantWithTimeMachine.minusNanos(nowOfInstantWithTimeMachine.getNano()));
assertEquals(ZonedDateTime.now(ZoneOffset.UTC).withNano(0), TimeMachine.nowOfZonedDateTime().withNano(0));
assertEquals(OffsetDateTime.now(ZoneOffset.UTC).withNano(0), TimeMachine.nowOfOffsetDateTime().withNano(0));
assertEquals(OffsetTime.now(ZoneOffset.UTC).withNano(0), TimeMachine.nowOfOffsetTime().withNano(0));
assertEquals(LocalDateTime.now(ZoneOffset.UTC).withNano(0), TimeMachine.nowOfLocalDateTime().withNano(0));
assertEquals(LocalDate.now(ZoneOffset.UTC), TimeMachine.nowOfLocalDate());
assertEquals(LocalTime.now(ZoneOffset.UTC).withNano(0), TimeMachine.nowOfLocalTime().withNano(0));
}
}