java 当 JUnit 5 没有 assertThat() 函数时,如何将 Hamcrest 与 JUnit 5 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43280250/
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
How do I use Hamcrest with JUnit 5 when JUnit 5 doesn't have an assertThat() function?
提问by Max
To use Hamcrest with JUnit 4 we use an assertThat()
function. However, JUnit 5 is no longer going to have an assertThat()
function. How do I use Hamcrest without an assertThat()
?
为了在 JUnit 4 中使用 Hamcrest,我们使用了一个assertThat()
函数。但是,JUnit 5 将不再具有assertThat()
功能。如何在没有 .的情况下使用 Hamcrest assertThat()
?
回答by Max
You have to make sure Hamcrest is included in the classpath and then use the assertThat()
function provided by Hamcrest. From the current JUnit 5 User Guide - Writing Tests Assertions,
您必须确保类路径中包含 Hamcrest,然后使用assertThat()
Hamcrest 提供的函数。从当前的JUnit 5 用户指南 - 编写测试断言,
JUnit Jupiter's org.junit.jupiter.Assertions class does not provide an assertThat() method like the one found in JUnit 4's org.junit.Assert class which accepts a Hamcrest Matcher. Instead, developers are encouraged to use the built-in support for matchers provided by third-party assertion libraries.
The following example demonstrates how to use the assertThat() support from Hamcrest in a JUnit Jupiter test. As long as the Hamcrest library has been added to the classpath, you can statically import methods such as assertThat(), is(), and equalTo() and then use them in tests like in the assertWithHamcrestMatcher() method below.
JUnit Jupiter 的 org.junit.jupiter.Assertions 类没有像 JUnit 4 的 org.junit.Assert 类中那样提供 assertThat() 方法,该方法接受 Hamcrest Matcher。相反,我们鼓励开发人员使用第三方断言库提供的对匹配器的内置支持。
以下示例演示了如何在 JUnit Jupiter 测试中使用来自 Hamcrest 的 assertThat() 支持。只要将 Hamcrest 库添加到类路径中,您就可以静态导入 assertThat()、is() 和 equalTo() 等方法,然后在测试中使用它们,例如下面的 assertWithHamcrestMatcher() 方法。
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.jupiter.api.Test;
class HamcrestAssertionDemo {
@Test
void assertWithHamcrestMatcher() {
assertThat(2 + 1, is(equalTo(3)));
}
}
Naturally, legacy tests based on the JUnit 4 programming model can continue using org.junit.Assert#assertThat."
当然,基于 JUnit 4 编程模型的遗留测试可以继续使用 org.junit.Assert#assertThat。”
回答by Grigory Kislin
See https://github.com/junit-team/junit5/issues/147:
见https://github.com/junit-team/junit5/issues/147:
you can use both, Hamcrest and AssertJ, in JUnit5. Both frameworks have a simple assertThat method, that you can import and use if wanted.
Currently, we do not plan to support these frameworks within our own Assertions to avoid the dependencies. Still, one can use them very well.
您可以在 JUnit5 中同时使用 Hamcrest 和 AssertJ。这两个框架都有一个简单的 assertThat 方法,您可以根据需要导入和使用。
目前,我们不打算在我们自己的断言中支持这些框架以避免依赖。尽管如此,人们仍然可以很好地使用它们。