Hamcrest泛型地狱#2:iterableWithSize给出errror"不适用于参数"

Ang*_*las 6 java generics testing junit hamcrest

在hamcrest(1.3.RC2,没有JUnit依赖)我没能使用 iterableWithSize().

我有一个(扩展)Iterator参数化Content像这样 EndResult<Content> contents = contentRepository.findAllByPropertyValue("title", "*content*");

这里EndResultpackage org.springframework.data.neo4j.conversion; public interface EndResult<R> extends Iterable<R> {...}Content是我的POJO.

现在,我认为这会奏效 assertThat(contents, iterableWithSize(1));

但它给了我错误: 方法断言Assert类型中的(T,Matcher)不适用于参数(EndResult <Content>,Matcher <Iterable <Object >>)

我也试过这些失败:

assertThat(contents, iterableWithSize(equalTo(1));

assertThat(contents, IsIterableWithSize.<EndResult<Content>>.iterableWithSize(1));

这些是我的进口:


    import static org.hamcrest.CoreMatchers.equalTo;
    import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
    import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertThat;
    import org.hamcrest.collection.IsIterableWithSize;

集合的hasSize按预期工作,但对于迭代器我甚至找不到一个有效的例子......

Mar*_*ers 13

它应该是

assertThat(contents, IsIterableWithSize.<Content>iterableWithSize(1));
Run Code Online (Sandbox Code Playgroud)

iterableWithSize是在您的组件类型键入Iterable,而不是迭代本身的具体类型.