Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我必须与当前时间进行比较。任何解决方案都会有所帮助,它必须在集成测试中运行 LocalDateTime localDateTime = LocalDateTime.now();。
LocalDateTime localDateTime = LocalDateTime.now();
一种不需要任何工具的解决方案是简单的依赖注入:你的代码依赖于告诉它当前时间的东西,那么为什么不传入一个时钟呢?
public class Clock { public LocalDateTime now() { return LocalDateTime.now(); } }
在你的测试中,你可以模拟那个时钟来返回你想要的任何东西。