是否有一个使用Hamcrest'describeMismatch'功能的JUnit断言版本? [英] Is there a version of JUnit assertThat which uses the Hamcrest 'describeMismatch' functionality?

查看:130
本文介绍了是否有一个使用Hamcrest'describeMismatch'功能的JUnit断言版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我尝试的每个JUnit版本中(最多4.8.1),一个失败的断言将显示如下错误消息:

In every version of JUnit I have tried (up to 4.8.1), a failing assertThat will display an error message that looks like:

预期:[describeTo ]

得到:[对象的字符串表示]

expected: [describeTo]
got: [String representation of object]

换句话说,它将显示对象的toString()而不是不匹配的描述来自Matcher。如果我使用org.hamcrest.MatcherAssert.assertThat中的assertThat,那么它将调用'describeMismatch'并显示更有用的错误消息。

In other words, it will display the toString() of the object instead of the mismatch description from the Matcher. If I use the assertThat from org.hamcrest.MatcherAssert.assertThat, then it will call 'describeMismatch' and display a more helpful error message.

我是否正确使用Junit或目前没有JUnit版本可以满足我的需求吗?那么大多数人都会使用Hamcrest断言吗?

Am I using Junit incorrectly or is there currently no version of JUnit that will do what I want? Do most people use the Hamcrest assertThat then?

推荐答案

简答:否。

据我所知,最新版本的Hamcrest(1.2)引入了类型签名,它与版本1.1不兼容,JUnit目前依赖它。我不确定Hamcrest的变化造成的损害程度(可以这么说),但JUnit团队似乎并不急于升级(参见 open issue )。

As far as I can tell, the most recent version of Hamcrest (1.2) has introduced type signatures which are incompatible with version 1.1, which JUnit currently depends on. I am not sure the extent of the damage (so to speak) created by the change in Hamcrest, but it does not appear that the JUnit team are in any hurry to upgrade (see the open issue).

我不完全确定我已经解决了我的问题问题,但我打算使用 MatcherAssert.assertThat()。这可能需要特定版本的JUnit(我相信junit-dep-xxx),它不会与Hamcrest发生类路径冲突。否则,当 assertThat()调用 describeMismatch()时,您可能会收到 NoSuchMethodErrors code>。

I am not entirely sure I have solved my issue, but I am planning to use MatcherAssert.assertThat(). This can require a specific release of JUnit (junit-dep-xxx I believe) which will not have classpath conflicts with Hamcrest. Otherwise you may receive NoSuchMethodErrors when assertThat() makes the call to describeMismatch().

这篇关于是否有一个使用Hamcrest'describeMismatch'功能的JUnit断言版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆