How can I mock java.time.LocalDate.now()
问题 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, when I'm running test 回答1: In your code, replace LocalDate.now() with LocalDate.now(clock);. You can then pass Clock.systemDefaultZone() for production and a fixed clock for testing. This is an example : First, inject the Clock . If you are using spring boot just do a : @Bean public Clock clock() { return Clock.systemDefaultZone(); } Second,