How to mock new Date() in java using Mockito

前端 未结 5 1118
失恋的感觉
失恋的感觉 2020-11-29 05:21

I have a function that uses the current time to make some calculations. I\'d like to mock it using mockito.

An example of the class I\'d like to test:



        
5条回答
  •  执笔经年
    2020-11-29 05:31

    You could do this by using PowerMock, which augments Mockito to be able to mock static methods. You could then mock System.currentTimeMillis(), which is where new Date() ultimately gets the time from.

    You could. I'm not going to advance an opinion on whether you should.

提交回复
热议问题