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
5
votes
2 answers

Java: How can I define an anonymous Hamcrest Matcher?

I'm trying use JUnit / Hamcrest to assert that a collection contains at least one element that my custom logic asserts is true. I'm hoping there's some kind of Matcher like 'anyOf' that takes a lambda (or anonymous class definition) where I can…
Alex Worden
  • 3,374
  • 6
  • 34
  • 34
5
votes
2 answers

How to validate correct values of elements inside arrays with Rest assured

I'm trying to validate some properties of my response as shown in the rest assured tutorial. The problem is that, when testing properties inside an array, I can verify, as in the example, that they appear, but not that they're matched to other…
user384729
  • 403
  • 1
  • 7
  • 16
5
votes
1 answer

Match a string to an enum name

Is there any clean way of testing that a given String matches an enum name? Right now I get Expected: is Actual: SUNDAY I want to avoid having to add .name() to each check assertThat("SUNDAY", is(SUNDAY.name())) Something…
user384729
  • 403
  • 1
  • 7
  • 16
5
votes
2 answers

Hamcrest matcher for a sublist/partial match?

Say I have an actual List [1, 2, 3, 4] and I want to check if it contains the sub-list [2, 3] (i.e. order is also important). Is there an existing matcher that does this? (There's a poorly-named hasItems method which only checks the actual list…
billc.cn
  • 7,187
  • 3
  • 39
  • 79
5
votes
0 answers

Better way to match list of strings using JsonPath and Hamcrest Matchers

I'm using JsonPath to test REST endpoints of my API. My test case looks something like this. First I create the new object class Item { String name; int id; List places; } String name = "Randomname" + new…
pdc
  • 134
  • 1
  • 9
5
votes
1 answer

Eclipse: The type org.hamcrest.core.CombinableMatcher$CombinableBothMatcher cannot be resolved. It is indirectly referenced from required .class files

I was working on a unit testing for a Spring MVC controller using TestNG and Mockito. I've included Hamcrest library in the Maven dependencies as shown below. What am I missing here? The error shows up when I use the following two…
Manoj Shrestha
  • 4,246
  • 5
  • 47
  • 67
5
votes
1 answer

Hamcrest Matchers for Java 8 LocalDate

I'd like to test if a java.time.LocalDate is within a fixed number of days of a test date and I'd like to use Hamcrest matchers if possible. Are there any matchers for Hamcrest (java) for working with Dates?
stewbis
  • 370
  • 3
  • 10
5
votes
2 answers

Mockery: test if argument is an array containing a key/value pair

How can I use mockery and hamcrest to assert that when a method of a mock object is called, one of the arguments passed to it is an array containing a key/value pair? For example, my test code might look like this: $mock =…
Jodes
  • 14,118
  • 26
  • 97
  • 156
5
votes
5 answers

testing the order of a collection

Given a list of objects I'd like to test that they return in the correct order, but I would like to not assert the entire object. For example I'd like to verify that they're in order by id 1, id 2, id 3, or in another case date mostRecent date…
xenoterracide
  • 16,274
  • 24
  • 118
  • 243
5
votes
1 answer

JUnit & hamcrest: could containsInAnyOrder() tell more about the mismatch?

While testing a Set with JUnit and Hamcrest Matchers I've noticed that Matchers.contains() method gives a pretty good clue on what's wrong with the test. On the other hand Matchers.containsInAnyOrder() difference report is almost useless. Here's the…
S. Pauk
  • 5,208
  • 4
  • 31
  • 41
5
votes
1 answer

Runtime error wen using Hamcrest Matchers in Android production code with proguard

I am writing an app that has a form with text fields that take numbers. To check whether the input is valid I decided to use Hamcrest Matchers. I defined: public static boolean checkThat(T actual, Matcher matcher) { return…
Gerhard Burger
  • 1,379
  • 1
  • 16
  • 25
5
votes
4 answers

How to use Mockito/Hamcrest in unit tests in Android Studio

I want to be able to make unit tests and instrumentation tests in Android Studio, and using Mockito in them. I'm using the new approach for tests in Android Studio 0.8. This is: building with gradle using official Android API for testing…
GaRRaPeTa
  • 5,459
  • 4
  • 37
  • 61
5
votes
1 answer

why does my hamcrest "contains" not work as expected?

I run this line: List ids = new List() ids.add("id1"); ids.add("id2"); assertThat(ids, contains("id1")); but strangely it returns "fail". how can I fix this?
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
5
votes
1 answer

Hamcrest Matchers - Assert Type of List

The Problem I'm currently trying to use Hamcrest Matchers to assert that the list type being returned is of a specific type. For example, let's say I have the following List that is being returned by my service call: List myList; I want…
Mike Koch
  • 1,540
  • 2
  • 17
  • 23
5
votes
2 answers

Override Mockito toString test output

I have written a custom Hamcrest Matcher to use with Mockito.doubleThat. I want to "override the toString()" method, so to speak, so that if there's a failure, the error is more verbose. Here's the JUnit failure trace: Argument(s) are…
durron597
  • 31,968
  • 17
  • 99
  • 158