将服务器日期时间迫使给定值,以执行没有新日期作为参数的JUNIT测试
我如何在我的Java应用程序的单元测试中模拟某个DateTime,独立于服务器的实际日期时间,以便该应用程序“相信”当前的日期时间是我强制的i,并且可以执行测试'现在我强制的日期时间等于吗?
PS:这个问题不是从其他问题中重复的
assertThat(newDate).isEqualTo(someDate);
How can I simulate a certain datetime inside a unit test of my java application, independent from the real date time of the server, such that the application 'believes' that the current datetime is the one I forced and the test can be executed 'as' now would be equal to the datetime I forced?
PS: The question is not duplicated from other question because my test has not the new datetime as parameter, in other words is not something like
assertThat(newDate).isEqualTo(someDate);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用依赖注入将某些内容传递到应用程序中,使您可以获取日期:
其中
calendar
只是一个抽象:对于测试,您可以具有返回所需日期的实现:
然后将其传递到应用程序:
在生产代码中,您需要一个使用系统时钟的实现:
同样,这只是有线的:
Use dependency injection to pass something into the application that lets you get the date:
where
Calendar
is just an abstraction:For tests, you can have an implementation that returns the date you want:
and you pass it into the application:
In the production code, you want an implementation that uses the system clock:
and again, this is just wired up: