6

在 hamcrest(1.3.RC2,没有 JUnit 依赖项)中,我使用失败iterableWithSize().

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

where EndResultis package org.springframework.data.neo4j.conversion; public interface EndResult<R> extends Iterable<R> {...} and Contentis a my Pojo。

现在,我认为这会起作用 assertThat(contents, iterableWithSize(1));

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

我也尝试了这些失败:

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

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

这些是我的进口:

    导入静态 org.hamcrest.CoreMatchers.equalTo;
    导入静态 org.hamcrest.collection.IsCollectionWithSize.hasSize;
    导入静态 org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
    导入静态 org.junit.Assert.assertEquals;
    导入静态 org.junit.Assert.assertThat;
    导入 org.hamcrest.collection.IsIterableWithSize;

集合的 hasSize 按预期工作,但对于迭代器,我什至找不到工作示例......

4

1 回答 1

13

它应该只是

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

iterableWithSize在your的组件类型Iterable上键入,而不是在 iterable 本身的具体类型上键入。

于 2012-03-14T18:27:54.920 回答