Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hamcrest hasItem and hasProperty, assert if a object with property value exists

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.equalTo;  assertThat(actual, hasItem(hasProperty("id", equalTo(1L)))); 

where actual is a POJO with id as Long.

I get,

The method assertThat(T, Matcher<? super T>) in the type MatcherAssert is not applicable for the arguments (List, Matcher<Iterable<? super Object>>)

From various documentation and other stackoverflow pages, it should be valid, but I get the above error.

like image 315
wenic Avatar asked Nov 20 '13 18:11

wenic


People also ask

What are Hamcrest assertions?

Hamcrest is used for unit testing in Java. The goal of Hamcrest is to make it easier to read and write test cases. We use Hamcrest to write the matcher objects that allow us to define the match rules declarative.

What is the Hamcrest matcher to test if a string contains another string?

Class StringContains. Tests if the argument is a string that contains a substring. Creates a matcher that matches if the examined String contains the specified String anywhere.

What is org Hamcrest matchers in?

Hamcrest is the well-known framework used for unit testing in the Java ecosystem. It's bundled in JUnit and simply put, it uses existing predicates – called matcher classes – for making assertions.

Why are there Hamcrest matchers?

Purpose of the Hamcrest matcher framework. Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit.


1 Answers

Try explicitly filling in the type parameter - assuming actual is a List<YourPojo>, try calling:

assertThat(actual, hasItem(Matchers.<YourPojo>hasProperty("id", equalTo(1L)))); 
like image 79
pobrelkey Avatar answered Sep 17 '22 21:09

pobrelkey