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

How can I use Hamcrest to check if each element in an array of doubles is "close" to each element in another array?

I would like to compare two arrays of doubles. Using vanilla JUnit, I can do: double[] a = new double[]{1.0, 2.0, 3.0}; double[] b = new double[]{1.0, 2.0, 3.0}; assertEquals(a, b, 1e-10); I would like to know how to do this using Hamcrest,…
rhaertel80
  • 8,254
  • 1
  • 31
  • 47
12
votes
4 answers

Any way to use Hamcrest matchers in production code?

I'd like to use hamcrest as sugar framework to use in if statements, not in the unit tests with asserts, but in raw production code. Something like if ( isNotEmpty(name) ) return //.... or if ( isEqual(name, "John")) return //... Just like…
Vitamon
  • 538
  • 7
  • 18
12
votes
1 answer

Mockito, argThat, and hasEntry

tl;dr: These tests don't compile because the type parameters don't match. What changes should I make to make them compile and run correctly? https://github.com/wesleym/matchertest I have some non-test code that calls into a service. It calls the…
Wesley
  • 10,652
  • 4
  • 37
  • 52
12
votes
4 answers

Check output of JsonPath with Hamcrest Matchers

I wrote Spring controller Junits. I used JsonPath to fetch all IDs from JSON using ["$..id"]. I have following as test method : mockMvc.perform(get(baseURL + "/{Id}/info", ID).session(session)) .andExpect(status().isOk()) // Success …
SuhasD
  • 728
  • 2
  • 7
  • 20
12
votes
4 answers

Junit best practice when asserting complex objects

I am writing a lot JUnit tests these days for a legacy system. Often I come to the question: What is the best way to assert complex Objects? Here is my current code public class SomeParserTest { @Test public void testParse() throws…
Zarathustra
  • 2,853
  • 4
  • 33
  • 62
12
votes
3 answers

Testing in Hamcrest that exists only one item in a list with a specific property

With Hamcrest we can easily test that exists at least one item in a list with a specific property, e.g. List myList = .... MatcherAssert.assertThat(myList, Matchers.hasItem(Matchers.hasProperty("fieldName", Matchers.equalTo("A funny…
JeanValjean
  • 17,172
  • 23
  • 113
  • 157
12
votes
3 answers

Android - espresso - clicking on a listview entry based on custom objects

Espresso is used for automatic testing my App. Edit: below you find a number of answers! How can I click (within an automated Espresso test script) on an entry in a long list of custom objects? In the Espresso documentation there is an example of…
tm1701
  • 7,307
  • 17
  • 79
  • 168
12
votes
2 answers

Rewriting "assertTrue" into "assertThat" in JUnit?

List list1 = getListOne(); List list2 = getListTwo(); Given the code above, I want to use a JUnit assertThat() statement to assert that either list1 is empty or that list1 contains all the elements of list2. The assertTrue…
r123454321
  • 3,323
  • 11
  • 44
  • 63
12
votes
1 answer

"Unchecked generic array creation for varargs parameter of type Matcher []" warning using CoreMatchers.allOf()

In my UT code, extract below, I see warning : Unchecked generic array creation for varargs parameter of type Matcher [] I have read in another stackoverflow answer about the problems using a generic parameter to a varargs…
k1eran
  • 4,492
  • 8
  • 50
  • 73
11
votes
3 answers

Hamcrest matcher to compare two arrays

I have a method that do sort an array, and I want to test it, I want to compare its result with the expected one, of course that I can do it using a for loop, but i'm asking if there is a Hamcrest matcher to do the comparison I have a class class…
octopus
  • 319
  • 1
  • 4
  • 16
11
votes
2 answers

Matching multiple properties in one Matcher

I need to write Matcher which will check multiple properties. For single property i've used: import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasProperty; import org.hamcrest.Matcher; import org.hamcrest.Matchers; …
Szympek
  • 183
  • 1
  • 2
  • 7
11
votes
1 answer

Do any tools use the hamcrest Factory annotation?

I sat down to write a matcher today and decided to take a quick look at the jmock documentation to refresh my memory on the process, and noticed a reference to the org.hamcrest.Factory annotation. The documentation for the annotation states. Marks…
sonstone
  • 697
  • 6
  • 9
11
votes
3 answers

Hamcrest equal collections

Is there a matcher in Hamcrest to compare collections for equality? There is contains and containsInAnyOrder but I need equals not bound to concrete collection type. E.g. I cannot compare Arrays.asList and Map.values with Hamcrest equals. Thanks in…
Andrey Minogin
  • 4,521
  • 6
  • 38
  • 60
11
votes
1 answer

Which dependencies do I need to use Mockito and JUnit in an Eclipse RCP Tycho project

This is my current test fragment: eclipse-test-plugin org.junit com.springsource.org.junit 4.7.0
oers
  • 18,436
  • 13
  • 66
  • 75
11
votes
1 answer

How should I use org.mockito.AdditionalMatchers.gt?

I'm trying to figure out how org.mockito.AdditionalMatchers works but I failed. Why is this test failing? import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; import static org.mockito.AdditionalMatchers.*; public class…
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820