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"));
669 questions
238
votes
15 answers

Getting "NoSuchMethodError: org.hamcrest.Matcher.describeMismatch" when running test in IntelliJ 10.5

I'm using JUnit-dep 4.10 and Hamcrest 1.3.RC2. I've created a custom matcher that looks like the following: public static class MyMatcher extends TypeSafeMatcher { @Override protected boolean matchesSafely(String s) { /*…
Noel Yap
  • 15,499
  • 17
  • 77
  • 123
155
votes
7 answers

Why should I use Hamcrest-Matcher and assertThat() instead of traditional assertXXX()-Methods

When I look at the examples in the Assert class JavaDoc assertThat("Help! Integers don't work", 0, is(1)); // fails: // failure message: // Help! Integers don't work // expected: is <1> // got value: <0> assertThat("Zero is one", 0, is(not(1))) //…
Peter Paul
153
votes
4 answers

How to assertThat something is null with Hamcrest?

How would I assertThat something is null? for example assertThat(attr.getValue(), is("")); But I get an error saying that I cannot have null in is(null).
user2811419
  • 1,763
  • 2
  • 12
  • 14
152
votes
5 answers

Checking that a List is not empty in Hamcrest

I was wondering if anyone knew of a way to check if a List is empty using assertThat() and Matchers? Best way I could see just use JUnit: assertFalse(list.isEmpty()); But I was hoping that there was some way to do this in Hamcrest.
Ian Dallas
  • 11,281
  • 18
  • 55
  • 80
126
votes
7 answers

Hamcrest compare collections

I'm trying to compare 2 lists: assertThat(actual.getList(), is(Matchers.containsInAnyOrder(expectedList))); But idea java: no suitable method found for assertThat(java.util.List,org.hamcrest.Matcher
xander27
  • 2,661
  • 7
  • 24
  • 37
123
votes
8 answers

How do I assert an Iterable contains elements with a certain property?

Assume I want to unit test a method with this signature: List getMyItems(); Assume MyItem is a Pojo that has many properties, one of which is "name", accessed via getName(). All I care about verifying is that the List, or any…
Kevin Pauli
  • 7,438
  • 13
  • 45
  • 67
120
votes
20 answers

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

While running junit test in eclipse I am getting this Exception: java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing I've added junit.jar library file. I've tried different versions of junit.jar: 4.4, 4.8, etc. How do I fix this Exception?
user2013948
  • 1,201
  • 2
  • 8
  • 3
89
votes
8 answers

How to use JUnit and Hamcrest together?

I can't understand how JUnit 4.8 should work with Hamcrest matchers. There are some matchers defined inside junit-4.8.jar in org.hamcrest.CoreMatchers. At the same time there are some other matchers in hamcrest-all-1.1.jar in org.hamcrest.Matchers.…
yegor256
  • 93,933
  • 106
  • 409
  • 558
76
votes
2 answers

assert collection does not contain item

Using the hamcrest library for Java, what's a nicely readable way to do the opposite of: assertThat(someCollection, hasItem(someItem)) I want to make sure someCollection does not contain item someItem
harschware
  • 11,609
  • 17
  • 50
  • 81
76
votes
2 answers

Multiple correct results with Hamcrest (is there an or-matcher?)

I am relatively new to matchers. I am toying around with hamcrest in combination with JUnit and I kinda like it. Is there a way, to state that one of multiple choices is correct? Something like assertThat( result, is( either( 1, or( 2, or( 3 ) ) ) )…
Mo.
  • 14,027
  • 13
  • 42
  • 57
71
votes
3 answers

Difference between hamcrest-library Matchers and hamcrest-core CoreMatchers

It looks like the hamcrest org.hamcrest.Matchers class is very similar to org.hamcrest.CoreMatchers (though it looks like Matchers has more). Why would I choose to use CoreMatchers (other than it looks like the class is slightly smaller), and why…
Jeff Storey
  • 53,386
  • 69
  • 224
  • 390
66
votes
3 answers

Is there a Hamcrest "for each" Matcher that asserts all elements of a Collection or Iterable match a single specific Matcher?

Given a Collection or Iterable of items, is there any Matcher (or combination of matchers) that will assert every item matches a single Matcher? For example, given this item type: public interface Person { public String getGender(); } I'd like…
E-Riz
  • 28,616
  • 7
  • 83
  • 119
63
votes
1 answer

How do Hamcrest's hasItems, contains and containsInAnyOrder differ?

Hamcrest provides a number of matchers for asserting the contents of a collection. All of these cases pass: Collection c = ImmutableList.of("one", "two", "three"); assertThat(c, hasItems("one", "two", "three"); assertThat(c, contains("one",…
Joe
  • 23,380
  • 9
  • 61
  • 75
60
votes
4 answers

Hamcrest number comparison using between

Is there a way in Hamcrest to compare a number within a number range? I am looking for something like this: assertThat(50L, is(between(12L, 1658L)));
saw303
  • 6,313
  • 2
  • 42
  • 68
58
votes
7 answers

How to count RecyclerView items with Espresso

Using Espresso and Hamcrest, How can I count items number available in a recyclerView? Exemple: I would like check if 5 items are displaying in a specific RecyclerView (scrolling if necessary).
Boris S.
  • 825
  • 1
  • 6
  • 17
1
2 3
44 45