java 在 JUnit 测试装置中设置时间和日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17229525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting time and date in JUnit test fixture
提问by hi5
As part of Java code some task needs to be done tomorrow in JUnit.
作为 Java 代码的一部分,明天需要在 JUnit 中完成一些任务。
For example there are 3 tasks:
例如有3个任务:
- task1 - done
- task2 - do it tomorrow
- task3 - then this will happen
- 任务 1 - 完成
- 任务 2 - 明天做
- 任务 3 - 那么这会发生
So as per requirement I need to set today's date as tomorrow in Java.
因此,根据要求,我需要在 Java 中将今天的日期设置为明天。
回答by Raedwald
The root of your problem is that the system clock (what gives you the time in a new Date ()
) is not giving you the time you need for your test.
问题的根源在于系统时钟(在 a 中为您提供时间的原因new Date ()
)没有为您提供测试所需的时间。
You can avoid this problem by introducing a level of indirection. Instead of having your code directly ask the Java API what the current time is, you have your code ask a Clock
object what the time is. In the real program your Clock
implementation uses the system clock. For your unit tests you use a FakeClock
, which says whatever time you want it to. You tell your code what Clock
to use using dependency injection. You have to write the Clock
interface (or abstract base class) and its concrete implementations yourself.
您可以通过引入一个间接级别来避免这个问题。不是让您的代码直接询问 Java API 当前时间是什么,而是让您的代码询问Clock
对象时间是什么。在实际程序中,您的Clock
实现使用系统时钟。对于您的单元测试,您使用 a FakeClock
,它表示您想要的任何时间。你告诉你的代码Clock
使用依赖注入来使用什么。您必须自己编写Clock
接口(或抽象基类)及其具体实现。
This approach requires you to alter your existing code, so it is easier to test. Or write it in the first place so it is easier to test. It's one of the tricks of unit testing.
这种方法要求您更改现有代码,因此更易于测试。或者把它写在首位,这样更容易测试。这是单元测试的技巧之一。
I'm working on some code at the moment that is time sensitive. My clock interface is simply
我正在处理一些时间敏感的代码。我的时钟界面很简单
public interface Clock {
long getCurrentTime ();
}
回答by Jon Skeet
The key is to extract the idea of "something which can give you the current time" as a dependency. (Along with the relevant time zone.)
关键是提取“可以为您提供当前时间的东西”的想法作为依赖项。(以及相关的时区。)
So instead of using new Date()
or System.currentTimeMillis()
you use a Clock
abstraction. For production code you inject an instance of this which uses the underlying system clock, but for testing you use a fake clock which you can control explicitly, to make it return whatever you want to.
因此,不要使用new Date()
或System.currentTimeMillis()
使用Clock
抽象。对于生产代码,您注入一个使用底层系统时钟的实例,但为了测试,您使用可以显式控制的假时钟,使其返回您想要的任何内容。
(It's really not clear exactly what you're trying to achieve, to be honest - but this approach is my standard approach to making time testable.)
(老实说,这真的不清楚你想要达到的目标——但这种方法是我让时间可测试的标准方法。)
回答by Stefan Birkner
回答by John B
Consider the following code on GitHub: DateSupplier&& DateController. DateSupplier
has a static method that returns the current date. DateController
allow you to manipulate the date value that is returned by DateSupplier
in a test environment. It is a JUnit Rule
and cleans up after itself.
考虑 GitHub 上的以下代码:DateSupplier&& DateController。DateSupplier
有一个返回当前日期的静态方法。DateController
允许您操作DateSupplier
在测试环境中返回的日期值。它是一个JUnit Rule
并自行清理。
This is a concrete implementation of the Clock
ideas from the other two answers. I have been using it in my projects for a year or so with good success. Any time I have code that would do new Date()
I call DateSupplier.getCurrentDate()
instead.
这是Clock
其他两个答案中思想的具体实现。我已经在我的项目中使用它一年左右并取得了成功。任何时候我有代码可以代替new Date()
我打电话DateSupplier.getCurrentDate()
。