我有一个List< Object>并希望在jUnit中验证列表是否具有某种类型的对象。我试过这个:
assertThat(myList,hasItem(isA(ExpectedClass.class)));
但是,我明白了:
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)
有人能告诉我如何构建合适的匹配器吗?
答案 0 :(得分:1)
问题是你使用了junit的断言而不是hamcrest的断言。
import org.hamcrest.MatcherAssert.assertThat;
工作正常。