Hamcrest: when iterableWithSize fails, it gives a bad message like "got: com.xxx.MyIterClass$1@1970ae0 "

In hamcrest (1.3.RC2, without JUnit dependencies) I cannot use iterableWithSize () with the SpringDataNeo4j library.

I have a (extension) a Iterator with a Content parameter like this

 EndResult<Content> contents = contentRepository.findAllByPropertyValue("title", "*"); 

where EndResult is

package org.springframework.data.neo4j.conversion; EndResult's open interface extends Iterable {...}

and Content is a @NodeEntity Pojo.

With the help of Mark Peters , I found out that I should call it that.

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

, since iterableWithSize is typed into the component type of your Iterable , and not the specific type of the iterative itself.

But when the test runs, I get

 java.lang.AssertionError: Expected: an iterable with size <2> got: org.springframework.data.neo4j.conversion.QueryResultBuilder$1@1 970ae0 

Trying to figure out 1) I'm doing something wrong, or 2) hamcrest or 3) Spring Data Neo4j has an error, I checked my object at hand, and it seems OK as 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 

Therefore, I suppose that perhaps he concludes that he has a problem that has a problem. Has anyone tried something similar with IsIterableWithSize?

Verification Code https://github.com/anodynos/SpringDataNeo4jTrials/blob/master/src/test/java/sdnTests/test/HamcrestIteratorSizeTest.java

0
source share
1 answer

You see this less useful message because you are using the JUnit version of assertThat . If you use the assertThat provided with hamcrest, it can better describe the mismatch.

Replace

 import static org.junit.Assert.assertThat; 

from

 import static org.hamcrest.MatcherAssert.assertThat; 
+1
source

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


All Articles