My class has a method, that tries to perform some operations and return nano time after all operations are completed. When testing this method I get a problem, that test result is always different (because depends on current time).
How to test return value correctly? Is there way without PowerMock?
Example class:
class ExampleClass {
public long performOperationsAndGetTimeNano(){
//some operations...
return Instant.now().getNano();
}
...
}
Example test:
@ExtendWith(MockitoExtension.class)
class ExampleTest{
private static final long EXPECTED_TIME = 555927900L;
private ExampleClass subject;
//init...
@Test
void performOperationAndGetTimeNano(){
long actualTime = subject.performOperationAndGetTimeNano(); //always different result
assertEquals(EXPECTED_TIME, actualTime); //always fails
}
}
Thanks for your answers!