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

后端 未结 7 1296
迷失自我
迷失自我 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 01:45

    You can simply pass the current date as function parameter.

    fun doSmthng(now: LocalDate = LocalDate.now()) {
        testAnotherService.doSmthng1(TestObj("my message!", now))
    }
    

    And in the test you can pass some specific date and assert on it. Idea is to inject the dependencies instead of creating explicitly in the function body.

提交回复
热议问题