I just test the example given in the javadoc of hamcrest, It seems hasItem does not work properly !
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.startsWith; import java.util.Arrays; import org.junit.Test;
public class Tester {
@Test
public void test(){
assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith( "bar")));
}
}
Comment #1
Posted on Apr 8, 2013 by Massive Hippo(No comment was entered for this change.)
Comment #2
Posted on Apr 8, 2013 by Massive HippoHi Mohamed,
I'm having trouble reproducing your issue. Please could describe the failure? Is it a compile failure, or a runtime failure. Perhaps you have a stack trace?
Regards, Tom
Comment #3
Posted on Apr 8, 2013 by Grumpy KangarooHi Tom,
When I execute this test, I am expecting it to succeed because the list contains the string "bar", but it fails.
Here is the stack trace :
java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:31) at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:14) at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55) at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:12) at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8) at Tester.test(Tester.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Comment #4
Posted on Apr 8, 2013 by Grumpy KangarooBut if I change to this : assertThat(Arrays.asList("bar", "foo"), hasItem(startsWith( "bar"))); It works, it seems like if it asserts only for the first element.
ps. I am using Java 7u17
Regards
Comment #5
Posted on Apr 8, 2013 by Massive HippoThis isn't actually an issue with Hamcrest, but one with the packaging of JUnit. You are picking up a version of the org.hamcrest.Matcher interface included in the JUnit jar.
If you are using JUnit with hamcrest, you should use the junit-dep.jar, which excludes the JUnit definitions of hamcrest classes. If you have difficulty with this, then simply upgrading your version of JUnit might help, as they recently packaged a newer version of hamcrest.
Regards, Tom
Comment #6
Posted on Apr 8, 2013 by Grumpy KangarooThanks a lot. I upgraded to Junit 4.11, it works fine
Regards,
Status: Fixed
Labels:
Java