如何一起使用 JUnit 和 Hamcrest?
我不明白 JUnit 4.8 应该如何与 Hamcrest 匹配器一起工作。 junit-4.8.jar< 中定义了一些匹配器/code>
在
org.hamcrest.CoreMatchers
中。同时,hamcrest-all-1.1.jar
在 org.hamcrest.Matchers
中。那么,该去哪里呢?我是否应该明确地将 hamcrest JAR 包含到项目中并忽略 JUnit 提供的匹配器?
特别是,我对 empty()
匹配器感兴趣,但在这些 jar 中找不到它。我还需要别的东西吗? :)
还有一个哲学问题:为什么 JUnit 将 org.hamcrest 包包含到自己的发行版中,而不是鼓励我们使用原始的 hamcrest 库?
I can't understand how JUnit 4.8 should work with Hamcrest matchers. There are some matchers defined inside junit-4.8.jar
in org.hamcrest.CoreMatchers
. At the same time there are some other matchers in hamcrest-all-1.1.jar
in org.hamcrest.Matchers
. So, where to go? Shall I explicitly include hamcrest JAR into the project and ignore matchers provided by JUnit?
In particular, I'm interested in empty()
matcher and can't find it in any of these jars. I need something else? :)
And a philosophical question: why JUnit included org.hamcrest
package into its own distribution instead of encouraging us to use original hamcrest library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您使用的 Hamcrest 版本大于或等于 1.2,则应使用
junit-dep.jar
。该 jar 没有 Hamcrest 类,因此您可以避免类加载问题。从 JUnit 4.11 开始,junit.jar 本身没有 Hamcrest 类。不再需要
junit-dep.jar
。If you're using a Hamcrest with a version greater or equal than 1.2, then you should use the
junit-dep.jar
. This jar has no Hamcrest classes and therefore you avoid classloading problems.Since JUnit 4.11 the
junit.jar
itself has no Hamcrest classes. There is no need forjunit-dep.jar
anymore.junit 提供了名为assertThat() 的新检查断言方法,该方法使用匹配器,并且应该提供更具可读性的测试代码和更好的失败消息。
为了使用它,junit 中包含了一些核心匹配器。您可以从这些开始进行基本测试。
如果您想使用更多匹配器,您可以自己编写它们或使用 hamcrest 库。
以下示例演示了如何在 ArrayList 上使用空匹配器:(
我在构建路径中包含了 hamcrest-all.jar)
junit provides new check assert methods named assertThat() which uses Matchers and should provide a more readable testcode and better failure messages.
To use this there are some core matchers included in junit. You can start with these for basic tests.
If you want to use more matchers you can write them by yourself or use the hamcrest lib.
The following example demonstrates how to use the empty matcher on an ArrayList:
(I included the hamcrest-all.jar in my buildpath)
不完全回答您的问题,但您绝对应该尝试 FEST-Assert 流畅的断言 API。它与 Hamcrest 竞争,但具有更简单的 API,只需要一次静态导入。以下是 cpater 使用 FEST 提供的代码:
编辑:Maven 坐标:
Not exactly answering your question, but you should definitely try FEST-Assert fluent assertions API. It's competing with Hamcrest, but has a much easier API with only one static import required. Here is the code provided by cpater using FEST:
EDIT: Maven coordinates:
另外,如果使用 JUnit 4.1.1 + Hamcrest 1.3 + Mockito 1.9.5,请确保不使用mockito-all。它包含 Hamcrest 核心类。请改用mockito-core。
以下配置有效:
Also, if JUnit 4.1.1 + Hamcrest 1.3 + Mockito 1.9.5 are being used, make sure mockito-all is not used. It contains Hamcrest core classes. Use mockito-core instead.
The below config works :
由于版本一直在变化,我发帖是为了让人们知道,自 2014 年 12 月 2 日起,http://www.javacodegeeks.com/2014/03/how-to-test-dependency- in-a-maven-project-junit-mockito-hamcrest-assertj.html 为我工作。我没有使用 AssertJ,只是使用这些:
Since versions are changing all the time, I'm posting to let people know that as of December 2, 2014, the instructions at http://www.javacodegeeks.com/2014/03/how-to-test-dependencies-in-a-maven-project-junit-mockito-hamcrest-assertj.html worked for me. I did not use AssertJ though, just these:
我猜这是因为他们希望
assertThat
成为 JUnit 的一部分。因此,这意味着Assert
类必须导入org.hamcrest.Matcher
接口,并且除非 JUnit 依赖于 Hamcrest,或者包含(至少部分的)汉克雷斯特。我想包含其中的一部分会更容易,这样 JUnit 就可以在没有任何依赖的情况下使用。I would guess that's because they wanted the
assertThat
to be part of JUnit. So that means theAssert
class has to import theorg.hamcrest.Matcher
interface and it can't do that unless JUnit either depends on Hamcrest, or includes (at least part of) Hamcrest. And I guess including part of it was easier, so that JUnit would be usable without any dependencies.2018 年使用最现代的库:
In 2018 using most modern libraries:
根据各自的 .xml 文件,JUnit-4.12 和 JUnit-Dep-4.10 都具有 Hamcrest 依赖项。
进一步调查表明,虽然依赖关系是在 .xml 文件中进行的,但源和类在 jar 中。这似乎是排除 build.gradle 中的依赖关系的一种方法......对其进行测试以保持一切干净。
仅供参考
Both JUnit-4.12 and JUnit-Dep-4.10 has Hamcrest dependencies according to the respective .xml files.
Further investigation shows that although the dependency was made in the .xml files, the source and classes in the jars. The seems to be a way of excluding the dependency in build.gradle ... testing it out to keep everything clean.
Just an f.y.i.