Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 943
Angelos Pikoulas Avatar asked Mar 14 '12 18:03

Angelos Pikoulas


1 Answers

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.

like image 153
Mark Peters Avatar answered Sep 28 '22 17:09

Mark Peters