Hamcrest generics hell #2 : iterableWithSize gives errror “is not applicable for the arguments”

和自甴很熟 提交于 2019-12-23 09:27:07

问题


In hamcrest (1.3.RC2, with no JUnit dependencies) I am failing using iterableWithSize().

I have an (extension of) an Iterator parametrized with Content like this EndResult<Content> contents = contentRepository.findAllByPropertyValue("title", "*content*");

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

Now, I would think that this would work assertThat(contents, iterableWithSize(1));

but it gives me the error : The method assertThat(T, Matcher) in the type Assert is not applicable for the arguments (EndResult< Content>, Matcher< Iterable< Object>>)

I also tried these failures :

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

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

These are my imports :


    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;

The hasSize for collections works as expected, but for iterators I cant even find a working example...


回答1:


It should just be

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

iterableWithSize is typed on the component type of your Iterable, not the concrete type of iterable itself.



来源:https://stackoverflow.com/questions/9707531/hamcrest-generics-hell-2-iterablewithsize-gives-errror-is-not-applicable-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!