加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – org.junit.Assert.assert好于org.hamcrest.MatcherAsser

发布时间:2020-12-14 05:15:18 所属栏目:Java 来源:网络整理
导读:我是新来的JUnit和Hamcrest,并希望最佳实践的建议,所以我可以决定哪些文件首先研究. 对于初学者,这些assertThat方法中哪一种更好? org.junit.Assert.assertThat(from junit-4.11.jar) org.hamcrest.MatcherAssert.assertThat(来自hamcrest-core-1.3.jar) 据
我是新来的JUnit和Hamcrest,并希望最佳实践的建议,所以我可以决定哪些文件首先研究.

对于初学者,这些assertThat方法中哪一种更好?

> org.junit.Assert.assertThat(from junit-4.11.jar)
> org.hamcrest.MatcherAssert.assertThat(来自hamcrest-core-1.3.jar)

据一位去年的一位,“JUnit has the assertThat method,but hamcrest has its own assertThat method that does the same thing.”.

据今年早些时候,Hamcrest “could potentially give better error messages because the matcher is called to describe the mismatch”.

很难说这些帖子比较了哪些版本的Junit和Hamcrest.所以我想要一个基于最新版本的推荐.

解决方法

这是几乎完全相同的事情.

JUnit的最新版本现在包括hamcrest.

Infact org.junit.Assert.assertT方法签名是

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 assert的两个细微差异,这使得它更有用:

>当匹配失败时,错误消息包括不同的“预期X但是是Y”.定制hamcrest匹配器可能包括更详细的信息,通过实现describeMismatch()
> assertThat签名在hamcrest使用T实际,Matcher超级T匹配器允许匹配器是超类型(如Matcher来比较整数和双精度).这通常没关系,但是当你需要它这是一个很好的功能.

所以使用org.hamcrest.MatcherAssert.assertThat

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读