本文介绍了混合Hamcrest和TestNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人将Hamcrest与TestNG集成在一起,以便其匹配器可以很容易地用于TestNG断言?

Has anyone integrated Hamcrest with TestNG so that its matchers can easily be used in TestNG assertions?

推荐答案

简而言之,到回答你的问题:你不需要将TestNG与Hamcrest集成。只需直接调用 org.hamcrest.MatcherAssert.assertThat(...),这会抛出 java.lang.AssertionError

In short, to answer your question: You don't need to integrate TestNG with Hamcrest. Just call org.hamcrest.MatcherAssert.assertThat(...) directly which throws java.lang.AssertionError.

背景

我通过Google找到了您的问题,想知道完全相同的问题。经过进一步的谷歌搜索,我没有找到任何令人满意的答案,所以我读了JUnit与Hamcrest集成的源代码。

I found your question via Google, wondering exactly the same issue. After further Googling, I didn't find any satisfying answers, so I read the source code for JUnit's integration with Hamcrest.

使用JUnit,Hamcrest集成通常用于调用:

With JUnit, Hamcrest integration is normally used by calling:

org.junit.Assert.assertThat(
    T actual,
    org.hamcrest.Matcher<? super T> matcher)

当我阅读源代码时,我发现它只是一个小包装器致电:

When I read the source code, I discovered it just a small wrapper to call:

org.hamcrest.MatcherAssert.assertThat(
    String reason,
    T actual,
    org.hamcest.Matcher<? super T> matcher)

此函数抛出 java.lang.AssertionError

这篇关于混合Hamcrest和TestNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:51