问题描述
在hamcrest(1.3.RC2,没有JUnit依赖项)中,我无法在SpringDataNeo4j库中使用iterableWithSize().
In hamcrest (1.3.RC2, with no JUnit dependencies) I am failing using iterableWithSize() with a SpringDataNeo4j library.
我有一个Iterator
(扩展名为),像这样用Content
参数化
I have an (extension of) an Iterator
parameterized with Content
like this
EndResult<Content> contents = contentRepository.findAllByPropertyValue("title", "*");
其中EndResult
是
包org.springframework.data.neo4j.conversion;公共接口EndResult扩展了Iterable {...}
package org.springframework.data.neo4j.conversion; public interface EndResult extends Iterable {...}
和Content
是@NodeEntity
Pojo.
在马克·彼得斯(Mark Peters)的帮助下,我了解到我应该这样称呼
assertThat(contents, IsIterableWithSize.<Content>iterableWithSize(2));
,因为iterableWithSize
是根据Iterable
的组件类型键入的,而不是可迭代本身的具体类型.
since iterableWithSize
is typed on the component type of your Iterable
, not the concrete type of iterable itself.
但是运行测试时,我会得到
But when test is run I get
java.lang.AssertionError: Expected:
an iterable with size <2>
got: org.springframework.data.neo4j.conversion.QueryResultBuilder$1@1970ae0
试图找出是1)我做错了什么,还是2)hamcrest或3)Spring Data Neo4j有一个错误,我检查了手边的对象,并且看起来像Iterable
一样还好:
Trying to figure out whether either 1) I am doing some thing wrong, or 2) hamcrest or 3) Spring Data Neo4j has an bug, I checked my object at hand, and it seems OK as an Iterable
:
public static int iterSize(Iterator iter){
int i=0;
while (iter.hasNext()){ i++;iter.next();}
return i;
}
public static int iterSize(Iterable iter) {return iterSize(iter.iterator());}
assertEquals("contents contain 2 items", 2, iterSize(contents)); // works OK
因此,我想它可能得出结论,认为它的棘手问题出了问题.有没有人尝试过与IsIterableWithSize类似的事情?
So I guess it possibly concludes that its hamcrest that has a problem. Has anyone tried anything similar with IsIterableWithSize ?
推荐答案
由于使用的是JUnit版本的assertThat
,因此您看到的帮助不大.如果使用hamcrest随附的assertThat
,则可以更好地描述不匹配情况.
You are seeing this less helpful message because you are using JUnit's version of assertThat
. If you use the assertThat
provided with hamcrest it is able to better describe the mismatch.
替换
import static org.junit.Assert.assertThat;
使用
import static org.hamcrest.MatcherAssert.assertThat;
这篇关于Hamcrest:当iterableWithSize失败时,它会给出一条错误消息,例如"got:com.xxx.MyIterClass$1@1970ae0&".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!