Mixing Hamcrest and TestNG
Asked Answered
S

3

31

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

Sycamine answered 7/7, 2009 at 15:24 Comment(0)
T
51

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.

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.

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)

This function throws java.lang.AssertionError.

Tadtada answered 11/5, 2013 at 6:28 Comment(2)
This is great except that TestNG has soft assertions which can't be used from Hamcrest.Vasoinhibitor
@Vasoinhibitor There's a workaround for soft assertionsThus
B
2

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

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.

hope it will help someone like me.

Briefcase answered 28/11, 2012 at 6:43 Comment(0)
D
0

What worked for me: 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.

Diecious answered 29/6, 2017 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.