- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我是 JUnit 和 Hamcrest 的新手,希望获得最佳实践建议,以便我可以决定首先研究哪些文档。
对于初学者来说,这些 assertThat
方法中哪个更好?
根据今年早些时候的消息,Hamcrest "could potentially give better error messages because the matcher is called to describe the mismatch" .
很难说在这些帖子中比较了 Junit 和 Hamcrest 的哪个版本。所以我想要一个基于最新发布版本的推荐。
最佳答案
几乎完全一样。
最新版本的 JUnit 现在包括 hamcrest。
其实org.junit.Assert.assertThat的方法签名是
public static <T> void assertThat(T actual,
org.hamcrest.Matcher<T> matcher)
您会注意到它使用了 hamcrest 匹配器。
您可能仍然希望包括您自己的 hamcrest 版本,因为 JUnit 更新不频繁,并且可能并不总是使用最新版本的 hamcrest。
根据maven pom,JUnit 4.11 使用 hamcrest 1.3,我认为这是撰写本文时最新的版本。
编辑我刚刚读了你的第二篇文章 http://blog.code-cop.org/2014/02/assert-or-matcherassert.html它描述了 hamcrest 中的 2 个细微差别 assertThat
这使它更有用:
describeMismatch()
,定制的 hamcrest 匹配器可能会包含更多关于到底出了什么问题的详细信息。 .assertThat
使用 T actual, Matcher<? super T> matcher
的 hamcrest 签名不同这允许匹配器成为父类(super class)型(如匹配器比较整数和 double )。这通常无关紧要,但当您需要它时,这是一个不错的功能。 所以使用org.hamcrest.MatcherAssert.assertThat
.
关于java - org.junit.Assert.assertThat 比 org.hamcrest.MatcherAssert.assertThat 好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27256429/
我正在使用 hamcrest 匹配器进行测试。为什么我一定要写 MatcherAssert.assertThat(a, Is.is(b)); 不仅仅是这个? assertThat(a, is(b));
我使用 JUnit 4.11 和 Hamcrest 1.1 进行编码测试,配置如下: junit junit 4.11 test org.hamcrest hamcres
我是 JUnit 和 Hamcrest 的新手,希望获得最佳实践建议,以便我可以决定首先研究哪些文档。 对于初学者来说,这些 assertThat 方法中哪个更好? org.junit.Assert.
我是一名优秀的程序员,十分优秀!