Mixing Hamcrest and TestNG

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, … Read more

How to test enum types?

For enums, I test them only when they actually have methods in them. If it’s a pure value-only enum like your example, I’d say don’t bother. But since you’re keen on testing it, going with your second option is much better than the first. The problem with the first is that if you use an … Read more

Hamcrest – what version to use? 1.3 or 2

EDIT: After several years the answer is to use the latest Hamcrest 2 version (2.2 from 17th October 2019). For additional details also refer to @dschulten’s answer. Following is my original answer which I leave as context to understand the problem and confusion around Hamcrest versions 1.3 and 2.0.0.0 back in the day. Based on … Read more

How to use Hamcrest to inspect Map items

Youu could just use contains or containsInAnyOrder. True, you’ll have to list all items in the List that way, but it works cleaner than hasItem: @SuppressWarnings(“unchecked”) @Test public void mapTest() { Map<String, List<MyItem>> map = new HashMap<String, List<MyItem>>(); map.put(“one”, asList(new MyItem(“1”), new MyItem(“one”))); assertThat(map, hasEntry(is(“one”), containsInAnyOrder(hasProperty(“name”, is(“one”)), hasProperty(“name”, is(“1”))))); }

How to verify Map size using Hamcrest

The good news There is a matcher that does exactly what you want in the current master branch of the JavaHamcrest project. You can call it like so: assertThat(mapMap, aMapWithSize(1)); And the bad news Unfortunately this matcher is not in the latest release of Hamcrest (1.3). [Update] And finally the very good news The aforementioned … Read more

Hamcrest Date Matchers

The OrderingComparison::greaterThan matcher will work on any type which is comparable to itself (it’s in the org.hamcrest.number package, but it’s not actually number-specific). Date is such a type.

NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11

This blog helped fix the same problem for me: Mixing JUnit, Hamcrest and Mockito: Explaining java.lang.NoSuchMethodError: org/hamcrest/Matcher.describeMismatch Inside the dependencies for Mockito and Junit, the author added excludes: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <exclusions> <exclusion> <artifactId>hamcrest-core</artifactId> <groupId>org.hamcrest</groupId> </exclusion> </exclusions> </dependency>

How to assertThat String is not empty

In hamcrest 1.3 you can using Matchers#isEmptyString : assertThat(string, not(isEmptyString())); In hamcrest 2.0 you can using Matchers#emptyString : assertThat(string, is(not(emptyString()))); UPDATE – Notice that : “Maven central has some extra artifacts called java-hamcrest and hamcrest-java, with a version of 2.0.0.0. Please do not use these, as they are an aborted effort at repackaging the different … Read more