Skip to content

Instantly share code, notes, and snippets.

@NemchinovSergey
Created April 2, 2021 14:49
Show Gist options
  • Save NemchinovSergey/0ffe5d8554be58ecdfb63274c1bb9134 to your computer and use it in GitHub Desktop.
Save NemchinovSergey/0ffe5d8554be58ecdfb63274c1bb9134 to your computer and use it in GitHub Desktop.
Mock new Date() and System.currentTimeMillis() with PowerMockito
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.time.Instant;
import java.util.Date;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Client.class, LegacyClass.class})
public class SystemTimeTest {
private final Date startDate = Date.from(Instant.parse("2010-01-01T00:00:00.00Z"));
private final Date endDate = Date.from(Instant.parse("2010-12-31T23:59:59.00Z"));
private final Date fakeNow = Date.from(Instant.parse("2010-12-03T10:15:30.00Z"));
@Before
public void init() throws Exception {
// mock `new Date()`
PowerMockito.whenNew(Date.class).withNoArguments().thenReturn(fakeNow);
System.out.println("Fake now: " + fakeNow);
// you can mock other constructors of Date
// PowerMockito.whenNew(Date.class).withAnyArguments().thenReturn(fakeNow);
// PowerMockito.whenNew(Date.class).withParameterTypes(long.class).withArguments(ArgumentMatchers.anyLong()).thenReturn(fakeNow);
// mock System.currentTimeMillis()
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.currentTimeMillis()).thenReturn(fakeNow.getTime()); // 2021.03.31 22:42:00 1617205235060L
System.out.println("Fake currentTimeMillis: " + System.currentTimeMillis());
}
@Test
public void newDate() {
System.out.println("new Date(): " + new Date());
Assert.assertTrue(Client.isBetweenDate(startDate, endDate));
}
@Test
public void currentTimeMillis1() {
System.out.println("currentTimeMillis: " + System.currentTimeMillis());
Assert.assertTrue(Client.isBetweenMillis(startDate, endDate));
}
@Test
public void currentTimeMillis2() {
System.out.println("currentTimeMillis: " + System.currentTimeMillis());
Assert.assertTrue(Client.isBetweenSystem(startDate, endDate));
}
@Test
public void legacyClass() {
LegacyClass legacyClass = new LegacyClass();
legacyClass.methodWithNewDate();
legacyClass.methodWithCurrentTimeMillis();
}
}
class Client {
public static boolean isBetweenDate(Date startDate, Date endDate) {
System.out.println("== Client isBetweenDate ===");
System.out.println("startDate is " + startDate);
System.out.println("endDate is " + endDate);
Date now = new Date();
System.out.println("Now is " + now);
return startDate.before(now) && endDate.after(now);
}
public static boolean isBetweenMillis(Date startDate, Date endDate) {
System.out.println("=== Client isBetweenMillis ==");
System.out.println("startDate is " + startDate.getTime());
System.out.println("endDate is " + endDate.getTime());
long now = System.currentTimeMillis();
System.out.println("Now is " + now);
return startDate.getTime() < now && endDate.getTime() > now;
}
public static boolean isBetweenSystem(Date startDate, Date endDate) {
System.out.println("=== Client isBetweenSystem ==");
System.out.println("startDate is " + startDate);
System.out.println("endDate is " + endDate);
Date now = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
System.out.println("Now is " + now);
return startDate.before(now) && endDate.after(now);
}
}
class LegacyClass {
public void methodWithNewDate() {
Date now = new Date();
System.out.println("LegacyClass new Date() is " + now);
}
public void methodWithCurrentTimeMillis() {
long now = System.currentTimeMillis();
System.out.println("LegacyClass System.currentTimeMillis() is " + now);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment