Java 为什么ISiterableContainingOrder Hamcrest matcher无法处理阵列?

Java 为什么ISiterableContainingOrder Hamcrest matcher无法处理阵列?,java,junit,hamcrest,Java,Junit,Hamcrest,很明显,除了使用.equals()之外,我对如何使用Hamcrest的ISiterableContainingOrder验证列表相等性感到困惑。我想在我的报告中看到Hamcrest的有用信息 为什么下面的测试甚至无法编译?其中有些人比其他人更违反直觉,至少对我来说是这样。我认为,一般的原则是,类型参数将被推断为什么,我传递给一个具有ValARGS签名的方法,所以它将考虑T的数组作为VARARGS的T,因此将产生一个匹配器基于T,而不是在T的数组,T的可迭代的,或类似的。 请给我一个解释,为什么一

很明显,除了使用
.equals()
之外,我对如何使用Hamcrest的
ISiterableContainingOrder
验证列表相等性感到困惑。我想在我的报告中看到Hamcrest的有用信息

为什么下面的测试甚至无法编译?其中有些人比其他人更违反直觉,至少对我来说是这样。我认为,一般的原则是,类型参数将被推断为什么,我传递给一个具有ValARGS签名的方法,所以它将考虑T的数组作为VARARGS的T,因此将产生一个匹配器基于T,而不是在T的数组,T的可迭代的,或类似的。 请给我一个解释,为什么一些最直观的行实际上甚至无法编译

特别是:

  • 3和4表示数组/列表的不对称性
  • 5和6表明我可以在列表中查找,而不是在数组中(?)
在标记为这样的行上的编译器警告对我来说更加神秘


@org.junit.Test
公共void testTest(){
字符串string1=“A”;
字符串string2=“B”;
字符串string3=“C”;
List list1=Lists.newArrayList(string1、string2、string3);
list2=Lists.newArrayList(string1,string2,“C”);
String[]array1=list1.toArray(新字符串[list1.size()]);
String[]array2=list2.toArray(新字符串[list2.size()]);
// -------------------------------------------------------------------------
//1)此注释行后的断言不编译
//Assert.assertThat(array2,isiterablecontainingorder.contains(array1));
// -------------------------------------------------------------------------
//2)此注释行后的断言不可编译
//Assert.assertThat(list2,IsiterableContainingOrder.contains(list1));
// -------------------------------------------------------------------------
//3)此注释行后的断言成功
Assert.assertThat(列表2,ISiterableContainingOrder.contains(array1));
// -------------------------------------------------------------------------
//4)此注释行后的断言未编译+有警告
//Assert.assertThat(array2,isiterablecontainingorder.contains(list1));
// -------------------------------------------------------------------------
//5)此注释行后的断言不可编译
//Assert.assertThat(array2,isiterablecontainingorder.contains(string1));
// -------------------------------------------------------------------------
//6)此注释行后的断言已编译但失败
Assert.assertThat(列表2,ISiterableContainingOrder.contains(string1));
// -------------------------------------------------------------------------
//7)此注释行之后的断言编译并成功
Assert.assertThat(列表2,
ISiterableContainingOrder.contains(string1、string2、string3));
// -------------------------------------------------------------------------
}

大量的惊奇:(
PS我意识到所有的惊讶都来自于我自己的无知,而不是别的。我应该复习一下泛型、类型推理、varargs等。我真的可以对此进行彻底的解释,并且我可能会在以后的几次中引用它

PPS我确实试着先读代码,但看了一会儿…;)不是为了假装:

@SuppressWarnings("unchecked")
@Factory
public static <E> Matcher<Iterable<? extends E>> contains(final Matcher<? super E> itemMatcher) {
    return contains(new ArrayList<Matcher<? super E>>(asList(itemMatcher)));
}
@SuppressWarnings(“未选中”)
@工厂

公共静态匹配器以下是答案。。。希望是正确的:)

这一个不编译,因为数组不实现可迭代

// 2) The assertion after this comment line DOES NOT COMPILE
// Assert.assertThat(list2, IsIterableContainingInOrder.contains(list1));
要使其正常工作,list2应该是一个
列表
,因为contains仅验证可迭代文件是否包含传递的项

// 3) The assertion after this comment line SUCCEEDS
Assert.assertThat(list2, IsIterableContainingInOrder.contains(array1));
通过,因为数组1被视为vararg

// 4) & 5) 
与1相同

// 6) The assertion after this comment line COMPILES but fails
Assert.assertThat(list2, IsIterableContainingInOrder.contains(string1));
包含集合中的所有项,而不仅仅是子集

// 7)
与3相似,但每个项目都通过了。

伙计,非常感谢您的支持。我真的很感激。既然你已经解释过了,这是有道理的。。。
// 6) The assertion after this comment line COMPILES but fails
Assert.assertThat(list2, IsIterableContainingInOrder.contains(string1));
// 7)