本文介绍了Hamcrest泛型地狱#2:iterableWithSize给出错误“不适用于参数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在hamcrest(1.3.RC2,没有JUnit依赖)
我没有使用 iterableWithSize()。



我有一个(扩展) Iterator 参数化为 Content 像这样
EndResult<内容> contents = contentRepository.findAllByPropertyValue(title,* content *);



其中 EndResult
package org.springframework.data.neo4j.conversion;
public interface EndResult< R>扩展了Iterable< R> {...}
内容是我的Pojo。



现在,我认为这将工作
assertThat(contents,iterableWithSize(1));




类型Assert中的方法assertThat(T,Matcher)
不适用
作为参数
(EndResult< Content>,Matcher< Iterable< Object >>)


我试过这些失败:

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



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



这些是我的进口:

 

导入静态org.hamcrest.CoreMatchers.equalTo;
导入静态org.hamcrest.collection。 IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
import static org.junit.Assert.a ssertEquals;
import static org.junit.Assert.assertThat;
import org.hamcrest.collection.IsIterableWithSize;

集合的hasSize按预期工作,但对于迭代器,我甚至找不到一个工作示例...

解决方案

它应该是

  assertThat(contents,IsIterableWithSize。< Content> iterableWithSize(1));在上键入

iterableWithSize 您的 Iterable 的组件类型,而不是iterable本身的具体类型。


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

解决方案

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.

这篇关于Hamcrest泛型地狱#2:iterableWithSize给出错误“不适用于参数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 13:47