liangzai-cool / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

hasItem is not working #195

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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")));
    }

}

Original issue reported on code.google.com by mohamed....@gmail.com on 8 Apr 2013 at 12:01

GoogleCodeExporter commented 8 years ago

Original comment by t.denley on 8 Apr 2013 at 7:02

GoogleCodeExporter commented 8 years ago
Hi 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

Original comment by t.denley on 8 Apr 2013 at 7:11

GoogleCodeExporter commented 8 years ago
Hi 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/Descriptio
n;)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)

Original comment by mohamed....@gmail.com on 8 Apr 2013 at 7:24

GoogleCodeExporter commented 8 years ago
But 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

Original comment by mohamed....@gmail.com on 8 Apr 2013 at 7:27

GoogleCodeExporter commented 8 years ago
This 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

Original comment by t.denley on 8 Apr 2013 at 8:52

GoogleCodeExporter commented 8 years ago
Thanks a lot.
I upgraded to Junit 4.11, it works fine

Regards,

Original comment by mohamed....@gmail.com on 8 Apr 2013 at 9:23