Mic*_*sky 15 java junit hamcrest
我是JUnit和Hamcrest的新手,并希望获得最佳实践建议,以便我可以决定首先学习哪些文档.
对于初学者来说,哪种assertThat方法更好?
根据去年的一个人的说法,"JUnit有assertThat方法,但是hamcrest有自己的assertThat方法做同样的事情." .
根据今年早些时候的人的说法,Hamcrest "可能会提供更好的错误消息,因为匹配器被称为描述不匹配".
在这些帖子中很难分辨出哪些版本的Junit和Hamcrest被比较了.所以我想根据最新发布的版本推荐.
dka*_*zel 22
这几乎完全相同.
JUnit的最新版本现在包括hamcrest.
实际上,org.junit.Assert.assert这是方法签名
public static <T> void assertThat(T actual,
org.hamcrest.Matcher<T> matcher)
Run Code Online (Sandbox Code Playgroud)
你会注意到使用hamcrest matchers.
您可能仍希望包含自己的hamcrest版本,因为JUnit不会经常更新,并且可能并不总是使用最新版本的hamcrest.
根据maven pom,JUnit 4.11使用hamcrest 1.3,我相信这是写作时最新的.
编辑
我刚刚阅读了你的第二篇文章http://blog.code-cop.org/2014/02/assert-or-matcherassert.html,它描述了hamcrest assertThat中的2个细微差别,使它更有用:
describeMismatch().assertThathamcrest中的签名是不同的T actual, Matcher<? super T> matcher,允许匹配器成为超类型(比如Matcher比较整数和双精度).这通常没关系,但是当你需要它时,这是一个很好的功能.所以使用org.hamcrest.MatcherAssert.assertThat.
JUnit 5 用户指南的摘录:
\n\n\n\n\n但是,JUnit Jupiter\xe2\x80\x99s
\norg.junit.jupiter.Assertions类不提供assertThat()类似于 JUnit 4\xe2\x80\x99sorg.junit.Assert类中接受 Hamcrest 的方法Matcher。相反,鼓励开发人员使用第三方断言库提供的对匹配器的内置支持。
我认为对于 JUnit 4,HamcrestassertThat是(官方)首选。
| 归档时间: |
|
| 查看次数: |
6059 次 |
| 最近记录: |