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
9
votes
1 answer

Hamcrest test that a map contains all entries from another map

I want to check that a map contains a certain set of entries. It is allowed to contain other entries that are not in expectedMap. I currently have the following assertion: assertThat(expectedMap.entrySet(),…
z7sg Ѫ
  • 3,153
  • 1
  • 23
  • 35
9
votes
10 answers

Mockito style anyXXX methods for unit testing

While unit testing some methods, there can be some scenarios where value of some parameters do not matter and can be any value. For example in this piece of code: public void method(String arg1, String arg2, int arg3){ if(arg1 == null) throw new…
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
9
votes
3 answers

onChildView and hasSiblings with Espresso

I am trying to access a button from a specific view. The same view is displayed 6 times. This is the code I am using. public void testTimeConfig(){ onData(withDesc("description")).onChildView(withId(R.id.positive)).perform(click()); } private…
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
9
votes
3 answers

Where is the declaration of JUnit Matcher#startsWith?

I was browsing the junit ExpectedExceptions' javadoc and I can't understand where the startsWith in their example comes from (marked HERE in the code). I checked the CoreMatcher utility class but could not find any static startsWith method. Where is…
assylias
  • 321,522
  • 82
  • 660
  • 783
8
votes
0 answers

Duplicate class org.hamcrest.BaseDescription found in the following modules,How to resolve?

Duplicate class org.hamcrest.BaseDescription found in the following modules: hamcrest-all-1.3.jar (org.hamcrest:hamcrest-all:1.3), hamcrest-core-1.3.jar (org.hamcrest:hamcrest-core:1.3) and mockito-all-2.0.2-beta.jar…
Roxas
  • 103
  • 4
8
votes
3 answers

ExpectedException cause of the cause?

I'm trying to verify that all my exceptions are correct. Because values are wrapped in CompletableFutures, the exception thrown is ExecutionException with cause being the exception that I would normally check. Quick example: void foo() throws A { …
Amir
  • 717
  • 2
  • 9
  • 21
8
votes
1 answer

Espresso - Matching an instance of a class

I'm currently trying to get Espresso to match a UIElement by it's class and its text since it currently does not have a resource ID (I know, I know...). I'm not sure what the proper syntax for this is since the Espresso documentation is fuzzy (I'm…
Los
  • 147
  • 1
  • 11
8
votes
2 answers

Hamcrest assertion of boolean fields with an "is" prefix getter method

We would like to assert that a list of custom objects contains an object with some of its fields having certain values, with a series of assertions like this assertThat(customObjectList, hasItem(hasProperty("someField",…
hammerfest
  • 2,203
  • 1
  • 20
  • 43
8
votes
2 answers

Hamcrest bug with either-or and null or incorrect usage?

I was shocked when something along the lines of: assertThat(null, either(is(nullValue())).or(notNullValue())); Fails with: java.lang.AssertionError: Expected: (is null or not null) but: was null at…
billc.cn
  • 7,187
  • 3
  • 39
  • 79
8
votes
2 answers

Is there a Matcher for recursively comparing directories?

I'm writing unit tests for IM- and exporting files. I need to test the resulting directory recursively byte by byte. I implemented a routine for flat directories by myself and know how to do this recursively also. But I don't want to reinvent the…
Andreas
  • 1,183
  • 1
  • 11
  • 24
8
votes
2 answers

how to implement a hamcrest matcher

I want to run this line of code: assertThat(contextPin.get(), equalTo(pinPage.getPinObjFromUi())); but I want to print to the log be informative meaning that I could know which fields were not equal. So I have thought to implement a matcher. I have…
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
8
votes
1 answer

Hamcrest Matchers.contains matcher not working (?)

I am trying to test if a collection has an item which toString() method returns a particular String. I tried do it using excellent Hamcrest matching classes, by combining contains with Matchers.hasToString, but somehow, its Matchers.contains is not…
aragaojcna
  • 95
  • 1
  • 2
  • 4
8
votes
2 answers

Why is it `Type` as return type in Java Generics and not `Type`?

I just wrote a simple JUnit Matcher for assertThat() which requires Generics, of course. By a bit of luck I found the right syntax for the return type of static Matcher not(Matcher m)..., although I don't understand why in the return type its…
towi
  • 21,587
  • 28
  • 106
  • 187
8
votes
2 answers

junit and hamcrest declaration

I am using junit at 4.10 and declared hamcrest-core at 1.3 and hamcrest-library at 1.3. My question is are hamcrest-library and hamcrest-core embedded in junit 4.10. what about junit 4.11? junit
user1772643
  • 615
  • 3
  • 11
  • 23
8
votes
3 answers

What is the recommended way to integrate Hamcrest into Eclipse's JUnit?

Is there a recommended way to integrate Hamcrest into the JUnit configuration in Eclipse? Currently Eclipse's JUnit comes with Hamcrest-core only. I want to edit that configuration to include Hamcrest-all. How should I do this?
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182