I am writing unit tests using TestNG. The problem is when I mock System.currentTimeMillis, it returns the actual value instead of the mocked one. Ideally, it should return 0L , but it returns the actual value. What should I do to proceed?
class MyClass{
public void func1(){
System.out.println("Inside func1");
func2();
}
private void func2(){
int maxWaitTime = (int)TimeUnit.MINUTES.toMillis(10);
long endTime = System.currentTimeMillis() + maxWaitTime; // Mocking not happening
while(System.currentTimeMillis() <= endTime) {
System.out.println("Inside func2");
}
}
}
@PrepareForTest(System.class)
class MyClassTest extends PowerMockTestCase{
private MyClass myClass;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
myclass = new MyClass();
}
@Test
public void func1Test(){
PowerMockito.mockStatic(System.class)
PowerMockito.when(System.currentTimeMillis()).thenReturn(0L);
myclass.func1();
}
}