How can I mock java.time.LocalDate.now()

后端 未结 7 1314
迷失自我
迷失自我 2020-12-01 01:02

In my test case, I need test time sensitive method, in that method we\'re using java 8 class LocalDate, it is not Joda.

What can I do to change time

7条回答
  •  不知归路
    2020-12-01 02:01

    You can refactor you code to make it test-friendly, for example, replace all invocations of LocalDate.now() with invocation of some method of custom mockable non-static class.

    Alternatively, you can use PowerMock's mockStatic.

提交回复
热议问题