2012-03-14 91 views
6

在hamcrest(1.3.RC2,没有JUnit的依赖) 我使用iterableWithSize().Hamcrest泛型地狱#2:iterableWithSize给errror“不适用于参数”

我有一个(延长)的失败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));

,但它给我的错误: 方法assertThat(T,匹配器) 在类型断言不适用 的参数 (EndResult <内容>,匹配器<可迭代<对象>>)

我也试过这些故障:

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

回答

13

它应该只是

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

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

+0

感谢马克 - 的确编译的时候它的好,但在运行测试时,我得到java.lang.AssertionError: 预期:可迭代的大小为<2> 有: 2012-03-14 18:38:03

+0

@Agelos:那么我想你最好修改你的代码或者测试!我正在回答Hamcrest问题,你要么做了错误的假设,要么破译了一些代码。我很抱歉,但我对Spring的了解不足以回答这个新问题。 – 2012-03-14 18:51:33

+0

但是由于EndResult 扩展了Iterable ,所以在{{(i;}}}代码的简单代码中还有什么可能是错误的? – 2012-03-14 19:02:51