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
18
votes
4 answers

How to verify that array contains object with rest assured?

For example, I have JSON in response: [{"id":1,"name":"text"},{"id":2,"name":"text"}]} I want to verify if a response contains a custom object. For example: Person(id=1, name=text) I found solution: Person[] persons =…
Yaryna
  • 340
  • 1
  • 5
  • 11
17
votes
4 answers

How to check for size AND presence of some items in collections in hamcrest

I'm using Hamcrest 1.3 and trying to achieve the following in a more compact way. Consider following test case: @Test public void testCase() throws Exception { Collection strings = Arrays.asList( "string one", "string…
mohamnag
  • 2,709
  • 5
  • 27
  • 40
17
votes
3 answers

hamcrest: how to match array is subset of another array?

Given that: int[] a = {1, 2, 3, 4}; int[] b = {1, 2, 3, 4, 5}; How to asser that "a" is a subset of "b" using hamcrest matchers? The following works assertThat(Arrays.asList(b), hasItems(a)); But since I am creating "a" from "b", I would prefer…
Kiran Mohan
  • 2,696
  • 6
  • 36
  • 62
16
votes
3 answers

How can I assert hasProperty with a Java Record?

I have a piece of code in a test that checks that a list of results contains certain properties, using Hamcrest 2.2: assertThat(result.getUsers(), hasItem( hasProperty("name", equalTo(user1.getName())) )); assertThat(result.getUsers(), hasItem( …
Christoffer Karlsson
  • 4,539
  • 3
  • 23
  • 36
16
votes
4 answers

How to assert Map contains Map with entry

I have a unit test that needs to check for a nested map value. I can get my assertion to work by pulling out the entry and matching the underlying Map, but I was looking for a clear way to show what the assertion is doing. Here is a very…
Jeff E
  • 658
  • 1
  • 8
  • 19
15
votes
2 answers

Using hamcrest to match Map contains entries of different types

Let's say I have a Map: Map map1 = new HashMap(); map1.put("foo1","foo1"); map1.put("foo2", Arrays.asList("foo2","bar2")); Now I'd like to use Hamcrest matchers to verify the Map's values. If this were a Map<…
acvcu
  • 2,476
  • 10
  • 40
  • 56
15
votes
5 answers

Junit Matcher for comparators?

For several days I am using now Junit's Matchers feature. Everything is working OK but I am looking for a matcher which uses a comparator for comparing and which does not rely on the objects equals methodes. I want to replace Assert.assertThat(one,…
14
votes
3 answers

JUnit5: How to assert several properties of an object with a single assert call?

I want to assert several properties of an object with a single assert call. With JUnit 4 and Hamcrest I would have written something like this: assertThat(product, allOf( hasProperty("name", is("Coat")), hasProperty("available",…
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
14
votes
5 answers

Is there a way to do deep comparison on a nested property with Hamcrest

I use hamcrest for most of my testing ,but have encountered a issue with it not being able to test a property one level down in the object graph .A snipped of my test case is below final List foos= fooRepository.findAll(spec); …
Gaurav Rawat
  • 1,294
  • 1
  • 25
  • 52
14
votes
1 answer

IDEA, Hamcrest and static imports

My maven project includes 186 187 org.hamcrest 188 hamcrest-all 189 1.3 190 When i type assertThat .. I'd…
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
13
votes
2 answers

Generics Hell - How do I pass a joda.DateTime to Hamcrest Matcher.greaterThan?

JodaTime has public final class DateTime extends BaseDateTime {...} which works its way up to public interface ReadableInstant extends Comparable Hamcrest has public static >…
Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118
13
votes
2 answers

Hamcrest assertThat ambiguous?

I got some samplecode from a college, imported the project and try to run the Tests: The method assertThat(Integer, Matcher) is ambiguous for the type MyClass Every assertThat is marked red with the same error-message so i tried to write the…
MartinL
  • 3,198
  • 4
  • 18
  • 20
13
votes
2 answers

Print response body when statusCode assert fails with restassured

I'm using Hamcrest to unit test a REST API. When I send a request, I often check for a 200 status code like this : public void myTest() { url = "route/to/my/rest/api/"; secured().when().get(url).then().statusCode(200); } But when I get a…
Matthias Beaupère
  • 1,731
  • 2
  • 17
  • 44
13
votes
2 answers

Why doesn't this assert work - assertThat(foo, is(not(null)));

This assertion compiles but fails even though I know for a fact that foo is not null: import static org.hamcrest.Matchers.is; // see http://stackoverflow.com/a/27256498/2848676 import static org.hamcrest.Matchers.not; import static…
Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
13
votes
3 answers

Want a JUnitMatchers AssertThat to test string contains 3 or more sub strings (currently using assertThat ... both ... and ....)

import static org.junit.matchers.JUnitMatchers.both; import static org.junit.matchers.JUnitMatchers.containsString; Now I check it contains foo and bar as below ... Assert.assertThat(text, both(containsString("foo")). …
k1eran
  • 4,492
  • 8
  • 50
  • 73