java 混合 Hamcrest 和 TestNG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1092996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:09:37  来源:igfitidea点击:

Mixing Hamcrest and TestNG

javatestnghamcrest

提问by Robert Munteanu

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

有没有人将 Hamcrest 与 TestNG 集成,以便其匹配器可以轻松地用于 TestNG 断言?

回答by kevinarpe

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.

简而言之,要回答您的问题:您不需要将 TestNG 与 Hamcrest 集成。org.hamcrest.MatcherAssert.assertThat(...)直接调用which throws java.lang.AssertionError

Background

背景

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:

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

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)

This function throws java.lang.AssertionError.

此函数抛出java.lang.AssertionError.

回答by Shobhit

If you are facing problem with empty method then I would suggest to add hamcrestfirst in dependency list. or import first hamcrest, it will solve the problem.

如果您遇到空方法的问题,那么我建议hamcrest首先在依赖项列表中添加。或先导入hamcrest,它会解决问题。

I was using TestNJ with rexsl(internally using Hamcrest) and it fails to find empty method. then I added rexsl first in dependency list, if you are adding library in class path you can try to add first the Hamcrest one.

我正在使用带有 rexsl 的 TestNJ(内部使用 Hamcrest),但它找不到空方法。然后我在依赖项列表中首先添加了 rexsl,如果您在类路径中添加库,您可以尝试先添加 Hamcrest 一个。

hope it will help someone like me.

希望它能帮助像我这样的人。

回答by R2D2

What worked for me: 1. http://search.maven.org/

什么对我有用: 1. http://search.maven.org/

  1. Search for 'java-hamcrest' the latest as for now is '2.0.0.0'

  2. find dependency for Gradle (in my case)

  3. Added compile 'org.hamcrest:java-hamcrest:2.0.0.0' to build.gradle in my project.

  1. 搜索“java-hamcrest”,目前最新的是“2.0.0.0”

  2. 找到 Gradle 的依赖项(在我的情况下)

  3. 在我的项目中将 compile 'org.hamcrest:java-hamcrest:2.0.0.0' 添加到 build.gradle。