public final class FakeTimeUtils extends Object
TimeUtils
, suitable for unit tests to carefully
control the time returned by TimeUtils.currentTimeMillis()
.
The fake TimeUtils
starts its clock at midnight January 1st, 2013,
and thereafter every call to currentTimeMillis()
either:
advanceMs(long)
to change the time.
A production "event" class may look something like this (javadocs omitted for conciseness):
public class FooEvent { static TimeUtils TIME = TimeUtils.getInstance(); ... private final long ts = TIME.currentTimeMillis(); public FooEvent(...) { ... } public long ts() { return ts; } @Override public String toString() { return "{" + TIME.hhmmssnnn(ts) + ... + "}"; } }Notice how the
TimeUtils
instance is stored in a static,
package-private field. Under production conditions, the ts
instance field is set to system time during construction.
A unit test for the class might look something like this, using the
default behavior of FakeTimeUtils
whereby the clock is advanced
10ms after every call to currentTimeMillis()
:
public class FooEventTest { private FakeTimeUtils fake; @Before public void setUp() { fake = FakeTimeUtils.getInstance(); FooEvent.TIME = fake.timeUtils(); } @Test public void basic() { long startTs = fake.getCurrent(); FooEvent e1 = new FooEvent(); FooEvent e2 = new FooEvent(); FooEvent e3 = new FooEvent(); assertEquals(AM_NEQ, startTs, e1.ts()); assertEquals(AM_NEQ, startTs + 10, e2.ts()); assertEquals(AM_NEQ, startTs + 20, e3.ts()); } }Alternatively, a unit test might wish to exert exact control over the time values returned:
public class FooEventTest { private static final long FIVE_K = 5000; private FakeTimeUtils fake; @Before public void setUp() { fake = FakeTimeUtils.getInstance(Advance.MANUAL); FooEvent.TIME = fake.timeUtils(); } @Test public void basic() { long startTs = fake.getCurrent(); FooEvent e1 = new FooEvent(); FooEvent e2 = new FooEvent(); FooEvent e3 = new FooEvent(); assertEquals(AM_NEQ, startTs, e1.ts()); assertEquals(AM_NEQ, startTs, e2.ts()); assertEquals(AM_NEQ, startTs, e3.ts()); fake.advanceMs(FIVE_K); FooEvent e4 = new FooEvent(); assertEquals(AM_NEQ, startTs + FIVE_K, e4.ts()); } }
Modifier and Type | Class and Description |
---|---|
static class |
FakeTimeUtils.Advance
Denotes how time will advance when
TimeUtils.currentTimeMillis() is invoked. |
Modifier and Type | Field and Description |
---|---|
static int |
TICK_SIZE
Increment clock by 10ms per call.
|
Modifier and Type | Method and Description |
---|---|
void |
advanceMs(long ms)
Advances the clock by the specified number of milliseconds.
|
long |
getCurrent()
Returns the current value of the clock, i.e. the value that
will be returned from the next invocation of
currentTimeMillis()
on the TimeUtils instance. |
static FakeTimeUtils |
getInstance()
Returns a fake TimeUtils instance with the behavior of advancing the
clock by 10ms after every call to
TimeUtils.currentTimeMillis() . |
static FakeTimeUtils |
getInstance(FakeTimeUtils.Advance advance)
Returns a fake TimeUtils instance with the specified behavior.
|
TimeUtils |
timeUtils()
Returns the patched time-utils instance to attach to the class
under test.
|
public static final int TICK_SIZE
public TimeUtils timeUtils()
public void advanceMs(long ms)
ms
- the number of milliseconds to advancepublic long getCurrent()
currentTimeMillis()
on the TimeUtils
instance.public static FakeTimeUtils getInstance()
TimeUtils.currentTimeMillis()
.public static FakeTimeUtils getInstance(FakeTimeUtils.Advance advance)
advance
- the required clock advancement behaviorCopyright © 2015. All Rights Reserved.