I have written a class called ErrorHandingFiniteStateMachine
, which basically does something like:
https://developers.google.com/safe-browsing/developers_guide_v3#RequestFrequencyHashes
Basically it has a function called ErrorHandlingFiniteStateMachine.update()
, which will be called when the caller class receives an error. It will then update the error state, which depends on:
- how many errors it has received;
- how much time it has elapsed since last error;
- and of course, the last error state.
Now my questions are:
- How to mock up the time elapsed in
mockito
, because the time can be very long, such as hours, and I definitely don't want to wait for several hours to finish a unit test. - Is there any standard tutorial or best practice to test a state machine with time-related features?
EDIT
As folks pointed out, I use System.currentTimeMillis
to remember the current timestamp. But BTW, the unit test is not supposed to know so much detailed info about the implementation, right?
MY SOLUTION
I finally wrapped System.currentTimeMillis()
in the ErrorHandlingFiniteStateMachine.getCurrentTimestamp()
, and make it package-private (no modifier), so it's accessible in my TestErrorHandlingFiniteStateMachine
. Then I used a mockito
spy
to mock the ErrorHandlingFiniteStateMachine.getCurrentTimestamp()