Java 汉克雷斯特日期匹配器
我需要在某个测试用例中的日期之前/之后进行测试。如果可能的话,我想用Java 汉克雷斯特日期匹配器,java,date,hamcrest,Java,Date,Hamcrest,我需要在某个测试用例中的日期之前/之后进行测试。如果可能的话,我想用 Hamcrest(Java)是否有处理日期的匹配器?如果是这样,我会在哪个包/类中找到特定的日期匹配器函数?有一些特定的hamcrest扩展,可以简化一些与日期相关的测试。请matcher将处理与自身类似的任何类型(它位于org.hamcrest.number包中,但实际上并不特定于数字)。Date就是这样一种类型。您可以看看将添加到hamcrest的新日期匹配器(我不知道什么时候会想到): 快速查看后,似乎会有一个新的包o
Hamcrest(Java)是否有处理日期的匹配器?如果是这样,我会在哪个包/类中找到特定的日期匹配器函数?有一些特定的hamcrest扩展,可以简化一些与日期相关的测试。请matcher将处理与自身类似的任何类型(它位于
org.hamcrest.number
包中,但实际上并不特定于数字)。Date就是这样一种类型。您可以看看将添加到hamcrest的新日期匹配器(我不知道什么时候会想到):
快速查看后,似乎会有一个新的包org.hamcrest.date包含:
- IsAfter
- 以前
- 是月日吗
- Issamedayofweek
- 这是一年中最好的一天
- 一小时
- 伊萨默顿
- Issamemin
- 伊萨姆月
- 伊萨默斯
- 伊萨迈耶
- 在
<dependency>
<groupId>org.exparity</groupId>
<artifactId>hamcrest-date</artifactId>
<version>1.1.0</version>
</dependency>
或
还有一个问题。它有几个日期
特定的匹配器(例如星期一()
)和其他适用于日期的匹配器,这是因为实现了可比的
(例如,请参见介于之间的()
,介于结论之间的()
)。该计划还支持JDK7版本的库中的Joda Time和JDK8版本中新的基于日期的类(主要是LocalDate
)
您可以执行如下断言:
final Date date = new Date();
assertThat(date, is(monday())); // JUnit style
given(date).assertIs(monday()); // Cirneco style
您可以对符合JDK7的项目使用以下依赖项:
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>java7-hamcrest-matchers</artifactId>
<version>0.7.0</version>
</dependency>
是奥齐莫夫
java7 hamcrest匹配器
0.7.0
或者,如果您使用的是JDK8,请执行以下操作
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>java8-hamcrest-matchers</artifactId>
<version>0.7.0</version>
</dependency>
是奥齐莫夫
java8 hamcrest匹配器
0.7.0
匹配器处理日期
s和其他可比较的
对象
以下是检查您的日期是否大于或等于的方法(≥) 到某个预期日期:
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.AnyOf.anyOf;
...
Date expectedMin = new Date()
// Execute the method being tested
Date resultDate = getDate();
// Validate
assertThat(resultDate, anyOf(greaterThan(expectedMin), equalTo(expectedMin)))
org.assertj:assertj核心:3.12.2
assertThat(actual)
.usingRecursiveComparison()
.ignoringFieldsMatchingRegexes("fieldToIgore")
.isEqualTo(expected);
谢谢。他们似乎已经抛弃了类,转而使用静态工厂方法,这使得真正稳定的链接变得不可能,但我已经尽可能地修复了它。这是真的。还有一些扩展提供了一些更易于阅读的方法。例如,提供了matcher
J7Matchers::after
这是OrderingCompar的别名ison::greaterThan
。在我看来,语义在单元测试中始终很重要,这就是为什么我通常更喜欢Google Truth提供的fulent方法,但有时我不得不在遗留项目中处理Hamcrest。这与主题有什么关系?
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.AnyOf.anyOf;
...
Date expectedMin = new Date()
// Execute the method being tested
Date resultDate = getDate();
// Validate
assertThat(resultDate, anyOf(greaterThan(expectedMin), equalTo(expectedMin)))
assertThat(actual)
.usingRecursiveComparison()
.ignoringFieldsMatchingRegexes("fieldToIgore")
.isEqualTo(expected);