Questions tagged [hamcrest]

Hamcrest is an open source library of constraint classes used to match objects and values, typically by other frameworks such as unit testing, mocking, or collections.

Hamcrest has been ported to Java, C++, Objective-C, Python, PHP and Erlang.

It is included as part of JUnit to make assertions more readable (It is also called fluent API). Compare

assertNotEquals(-1, userName.indexOf("bob"));

to

assertThat(userName, containsString("bob"));
710 questions
22
votes
2 answers

Why does hamcrest say that a byte 0 is not equal to an int 0?

Consider the following test case using standard JUnit asserts and hamcrest's assertThat: byte b = 0; int i = 0; assertEquals(b, i); // success assertThat(b, equalTo(i)); // java.lang.AssertionError: Expected: <0> but: was <0> if (b == i) { …
sina
  • 1,817
  • 1
  • 18
  • 42
22
votes
3 answers

Java 8 - retry a method until a condition is fulfilled (in intervals)

I want to create a class that can run a method until a condition about the return value is fulfilled. It should look something like this methodPoller.poll(pollDurationSec, pollIntervalMillis) .method(dog.bark()) …
Bick
  • 17,833
  • 52
  • 146
  • 251
22
votes
2 answers

Hamcrest matcher for checking return value of method in collection

hasProperty can be used with hasItem to check for the value of a given property, eg: Matcher hasName = MatchershasProperty("name", is("Winkleburger")); assertThat(names, hasItem(hasName)); This is fine when name is a property, ie: there is…
tekumara
  • 8,357
  • 10
  • 57
  • 69
22
votes
4 answers

Hamcrest with MockMvc: check that key exists but value may be null

I'm doing some tests with MockMvc, and I want to validate the structure of a JSON response. Specifically, I want to make sure that the key to an attribute exists, and that the value is of a certain type or null. { "keyToNull": null, # This may…
nebulabrot
  • 465
  • 1
  • 6
  • 13
21
votes
7 answers

Java Hamcrest : Collection contains item of type

I'd like to assert that List contains a member of type TestAchievement. Here's my assertion: List achievements; // Populated elsewhere assertThat(achievements,hasItem(isA(TestAchievement.class))); This doesn't compile,…
Marty Pitt
  • 28,822
  • 36
  • 122
  • 195
21
votes
4 answers

Hamcrest: How to instanceOf and cast for a matcher?

Question Assume the following simple test: @Test public void test() throws Exception { Object value = 1; assertThat(value, greaterThan(0)); } The test won't compile, because "greaterThan" can only be applied to instances of type Comparable.…
yankee
  • 38,872
  • 15
  • 103
  • 162
21
votes
2 answers

Hamcrest When to use Is or equalTo

I'm new using hamcrest. While I'm discovering how to use it I have been a doubt about when to use is or equalTo. Is there any difference between is and equalTo, although it is conceptually or ocasionally? It seems to behave the same. …
Pau
  • 14,917
  • 14
  • 67
  • 94
21
votes
5 answers

opposite of contains in hamcrest

What is the opposite of contains? List list = Arrays.asList("b", "a", "c"); // should fail, because "d" is not in the list expectedInList = new String[]{"a","b", "c", "d"}; Assert.assertThat(list,…
pihentagy
  • 5,975
  • 9
  • 39
  • 58
21
votes
3 answers

Is org.junit.Assert.assertThat better than org.hamcrest.MatcherAssert.assertThat?

I'm new to JUnit and Hamcrest and would like best-practice advice so I can decided which documentation to study first. For starters, which of these assertThat methods is better? org.junit.Assert.assertThat (from…
Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
21
votes
2 answers

Test that either one thing holds or another in AssertJ

I am in the process of converting some tests from Hamcrest to AssertJ. In Hamcrest I use the following snippet: assertThat(list, either(contains(Tags.SWEETS, Tags.HIGH)) .or(contains(Tags.SOUPS, Tags.RED))); That is, the list may be either that…
Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
21
votes
6 answers

assertThat - hamcrest - check if list is sorted

Ok I think its going to be a short question. I have an ArrayList that I sorted by date, of course I see it works but I would also like to write a test for it. I want to check if next value (date) in my list is lower then previous one. I am able to…
lukaszrys
  • 1,656
  • 4
  • 19
  • 33
21
votes
4 answers

Mockito, JUnit, Hamcrest, Versioning

By default, the required version of Hamcrest for: JUnit 4.11 Hamcrest 1.3 Mockito-core 1.9.5 Hamcrest 1.1 There were not insiginifcant API changes between Hamcrest 1.1 and 1.3. Currently my test cases attempt to run JUnit 4.11 with Hamcrest…
durron597
  • 31,968
  • 17
  • 99
  • 158
20
votes
4 answers

How do I compare doubles using JUnit and Hamcrest?

I'm writing a unit test using JUnit and Hamcrest. I have been comparing double values using: assertThat(result, is(0.5)); However, I'm now needing to compare calculated values and I don't want to have to compare against the full double value.…
Tim B
  • 40,716
  • 16
  • 83
  • 128
20
votes
3 answers

Is there a Hamcrest matcher to check that a Collection is neither empty nor null?

Is there a Hamcrest matcher which checks that the argument is neither an empty Collection nor null? I guess I could always use both(notNullValue()).and(not(hasSize(0)) but I was wondering whether there is a simpler way and I missed it.
jhyot
  • 3,733
  • 1
  • 27
  • 44
19
votes
2 answers

Generics hell: hamcrest matcher as a method parameter

So, let's have a list of strings and a function that takes a Hamcrest matcher and returns a result of the matches() method of the provided matcher: public boolean matchIt(final Matcher> matcher) { final List lst =…
Jan Dudek
  • 271
  • 3
  • 7