6

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

Angelos Pikoulas
  • 1,002
  • 1
  • 14
  • 21

1 Answers1

13

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.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • Thanks Mark - indeed compile time its ok, but when test is run I get java.lang.AssertionError: Expected: an iterable with size <2> got: – Angelos Pikoulas Mar 14 '12 at 18:38
  • @Agelos: Then I guess you'd better fix either your code or your test! I was answering the Hamcrest question, you've either got a wrong assumption or some code is broken. I'm sorry but I don't know enough about Spring to answer the new question. – Mark Peters Mar 14 '12 at 18:51
  • But since EndResult extends Iterable, what else could be wrong in the trivial {next();i++} code ? – Angelos Pikoulas Mar 14 '12 at 19:02
  • @Agelos: Are you trying to get me to help you debug code that you haven't given? I'm not going to be of much help. If you are still having a problem either edit your question or ideally ask a new one, since I think I've quite definitively helped you solve your original problem. – Mark Peters Mar 14 '12 at 19:13
  • absolutely you've helped me greaty on the original question, and I thank you! Check my new question ,and BTW, if by any chance you know any Hamcrest members in here that could help, it 'd be neat! http://stackoverflow.com/questions/9727711/hamcrest-generics-iterablewithsize-gives-errror-is-not-applicable-for-the-arg – Angelos Pikoulas Mar 15 '12 at 20:34