Hamcrest匹配器的重载冲突 [英] Conflicting overloads for Hamcrest matcher

查看:306
本文介绍了Hamcrest匹配器的重载冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

匹配器 IsIterableContainingInAnyOrder 对于静态工厂方法有两个重载 containsInAnyOrder (两者都有返回类型 Matcher< java.lang.Iterable<?extends T>> ; ):

The matcher IsIterableContainingInAnyOrder has two overloads for the static factory method containsInAnyOrder (both have the return type Matcher<java.lang.Iterable<? extends T>>):


  1. containsInAnyOrder(java.util.Collection< Matcher<?super T>> itemMatchers)

  2. containsInAnyOrder(Matcher<?super T> ... itemMatchers)

  1. containsInAnyOrder(java.util.Collection<Matcher<? super T>> itemMatchers)
  2. containsInAnyOrder(Matcher<? super T>... itemMatchers)

现在考虑以下计划:

import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

import java.util.Arrays;

import org.junit.Test;

public class SomeTest {

    @SuppressWarnings("unchecked")
    @Test
    public void foo() {
        assertThat(Arrays.asList("foo","bar"), 
                       containsInAnyOrder(equalTo("foo"), equalTo("bar")));
    }

}

将此作为JUnit测试执行时它按预期通过。它使用上面显示的 containsInAnyOrder 的第二个重载。

When executing this as a JUnit test, it passes, as expected. It uses the second overload of containsInAnyOrder shown above.

现在,当我将断言更改为此时(与第一次重载的文档):

Now, when I change the assertion to this (which exactly matches the example given in the documentation of the first overload):

assertThat(Arrays.asList("foo","bar"), 
           containsInAnyOrder(Arrays.asList(equalTo("foo"), equalTo("bar"))));
                              ^^^^^^^^^^^^^^

it doesn' t编译了,因为现在编译器推断返回类型 containsInAnyOrder

it doesn't compile anymore, because now the compiler infers the return type of containsInAnyOrder to be

Matcher<Iterable<? extends List<Matcher<String>>>>

似乎编译器仍然选择第二个重载。如果它使用了第一个,那么示例应该有效。为什么它会像这样?我怎样才能做到这一点?

It seems like the compiler still chooses the second overload. If it used the first one, the example should work. Why does it behave like this? How can I make this work?

我正在使用Hamcrest 1.3和Oracle Java 1.7。

I am using Hamcrest 1.3 and Oracle Java 1.7.

推荐答案

它实际上匹配两个重载方法。我不确定为什么选择第一个,但你可以提供一个提示,让它选择正确的方法。

It actually matches both overloaded methods. I'm not sure why exactly the first one is chosen, but you can provide a hint to make it choose the correct method.

通过将参数强制转换为 Collection

By casting the argument to Collection:

assertThat(Arrays.asList("foo","bar"),
        containsInAnyOrder((Collection)Arrays.asList(equalTo("foo"), equalTo("bar"))));

或将通用类型T指定为< String> (不要使用静态导入):

or by specifying the generic type T as <String> (don't work with static import, though):

assertThat(Arrays.asList("foo","bar"),
        IsIterableContainingInAnyOrder.<String>containsInAnyOrder(Arrays.asList(equalTo("foo"), equalTo("bar"))));

这篇关于Hamcrest匹配器的重载冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆