本文介绍了如何模拟当前日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码中有一个方法可以验证当日营业。
I have a method in my code which verifies that current day is business. it checks database calendar for this.
方法如下:
public boolean iBusinessDayToday() {
LocalDate today = LocalDate.now();
//logic with today
}
我需要为
我希望单元测试不依赖于工作日。
I want that unit test doesn't depends in week day.
您认为需要什么使用powerMockito模拟 LocalDate.now()
的问题?
What do you think need this issue to use powerMockito to mock LocalDate.now()
?
推荐答案
另一种方法是将时钟传递给该方法:
An alternative would be to pass the clock to the method:
public boolean iBusinessDayToday() {
return iBusinessDayToday(Clock.systemDefaultZone());
}
@VisibleForTesting
boolean iBusinessDayToday(Clock clock) {
LocalDate today = LocalDate.now(clock);
//logic with today
}
现在您可以测试第二种方法并传递-无需使用模拟框架。
Now you can test the second method and pass a a fixed clock - no need to use a mocking framework.
这篇关于如何模拟当前日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!