java testing: accelerate time to test timeouts?
A way to do this would be to make your own interface that provides a thin wrapper around Timer
. You then program towards the interface everywhere in your code. After that, you make two implementations of the interface. The first is the expected implementation that connects to a real Timer
object like you have currently. The other is one that you can use for testing. In this implementation you mock the functionality of a Timer
, but you have full control of how the events are triggered and how long they take. You could scale the duration as suggested by @aioobe or you could make a backing queue that could rapidly fire events so that there is no wasted time.
The point is that you don't have to make changes to the real code and use Dependency Injection to make changes needed for testing.