user3923124
user3923124

Reputation: 299

Hamcrest Matcher for an item in an array of a certain class

I have a List<Object> and want to verify in jUnit that the list has an object of a certain type. I tried this:

assertThat(myList,hasItem(isA(ExpectedClass.class)));

But, I get this:

java.lang.AssertionError: 
Expected: a collection containing null
     but: 
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
    at org.junit.Assert.assertThat(Assert.java:865)
    at org.junit.Assert.assertThat(Assert.java:832)

Could someone tell me how to build the appropriate matcher?

Upvotes: 2

Views: 782

Answers (1)

Carl Manaster
Carl Manaster

Reputation: 40336

The problem is that you've used junit's assertThat instead of hamcrest's.

import org.hamcrest.MatcherAssert.assertThat; 

works fine.

Upvotes: 1

Related Questions