I want to unit test some code that calls System.currentTimeMillis()
. As this answer points out, a good way to do this is to replace calls to System.currentTimeMillis()
with Clock.getInstance().currentTimeMillis()
. Then, you can perform dependency injection on Clock.getInstance()
, for example to replace it with a mock in unit testing.
So, my question is a follow-up to that. How do you configure Spring Boot to inject Clock.getInstance()
at runtime?
If possible, I'd prefer to do this with annotations instead of XML.
Also, if possible, I'd like to do this in such a way that I can simply use @Mock
and @InjectMocks
with MockitoJUnitRunner
to inject a mock clock into a unit test.