问题
In hamcrest (1.3.RC2, with no JUnit dependencies) I am failing using iterableWithSize() with a SpringDataNeo4j library.
I have an (extension of) an Iterator
parameterized with Content
like this
EndResult<Content> contents = contentRepository.findAllByPropertyValue("title", "*");
where EndResult
is
package org.springframework.data.neo4j.conversion; public interface EndResult extends Iterable {...}
and Content
is a a @NodeEntity
Pojo.
With the help of Mark Peters I learned that I should call it like this
assertThat(contents, IsIterableWithSize.<Content>iterableWithSize(2));
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
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
So I guess it possibly concludes that its hamcrest that has a problem. Has anyone tried anything similar with IsIterableWithSize ?
The test code is https://github.com/anodynos/SpringDataNeo4jTrials/blob/master/src/test/java/sdnTests/test/HamcrestIteratorSizeTest.java
回答1:
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.
Replace
import static org.junit.Assert.assertThat;
with
import static org.hamcrest.MatcherAssert.assertThat;
来源:https://stackoverflow.com/questions/9727711/hamcrest-when-iterablewithsize-fails-it-gives-a-bad-message-like-got-com-xxx