Generic Hambrest Hell # 2: iterableWithSize gives error "not applicable for arguments"

In hamcrest (1.3.RC2, without JUnit dependencies) I do not use iterableWithSize().

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

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

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

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

I also tried these glitches:

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

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

This is my import:

     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 for collections works as expected, but for iterators I can't even find a working example ...

+6
source share
1 answer

It should be easy

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

iterableWithSize introduces the component type of your Iterable , not the specific type of iteration.

+13
source

Source: https://habr.com/ru/post/910736/


All Articles