调用了错误的重载方法

时间:2017-03-29 23:42:22

标签: java junit overloading matcher hamcrest

我正在尝试使用包含Matcher的具有此签名的List参数:

public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> 
    contains(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) {

但是,我的代码实际上最终会使用此签名调用varargs版本:

public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> 
    contains(E... items) {

为什么会这样?我使用与Hamcrest文档完全相同的代码。我正在使用JUnit 4.8.1和Hamcrest 1.3。

完整代码:

package com.mycompany.myapp;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;

import java.util.Arrays;
import java.util.List;

import org.hamcrest.Matcher;
import org.junit.Assert;
import org.junit.Test;

public class Test1
{
    @Test
    public void test() {
        Assert.assertThat(Arrays.asList("foo", "bar"),
            contains(Arrays.asList(equalTo("foo"), equalTo("bar"))));
    }
}

测试应该通过,但是失败了:

java.lang.AssertionError: 
Expected: iterable containing [<["foo", "bar"]>]
     got: <[foo, bar]>

    at org.junit.Assert.assertThat(Assert.java:778)
    at org.junit.Assert.assertThat(Assert.java:736)
    at com.mycompany.myapp.Test1.test(Test1.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

0 个答案:

没有答案