java - 为什么 IsIterableContainingInOrder Hamcrest 匹配器无法处理数组?

标签 java junit hamcrest

除了使用 .equals() 之外,我显然对如何使用 Hamcrest 的 IsIterableContainingInOrder 来验证 List 相等性感到困惑。我希望在我的报告中看到 Hamcrest 的有用信息。

为什么下面的测试甚至无法编译?其中一些比其他的更违反直觉,至少对我而言。我认为,我得到了一般原则,即类型参数将被推断为我传递给具有可变参数签名的方法的内容,因此它将 T 的数组视为 T 的可变参数,因此将生成基于 T 而不是基于 T 的 Matcher T 的数组、T 的可迭代对象或类似的东西。

请解释一下为什么一些最直观的行实际上什至无法编译。

特别是:

  • 3和4展示了array/List的不对称性
  • 5 和 6 表明我可以查看列表而不是数组(?)

在标记为这样的行上的编译器警告对我来说更加神秘。


@org.junit.Test
public void testTest() {
  String string1 = "A";
  String string2 = "B";
  String string3 = "C";

  List<String> list1 = Lists.newArrayList(string1, string2, string3);
  List<String> list2 = Lists.newArrayList(string1, string2, "C");

  String[] array1 = list1.toArray(new String[list1.size()]);
  String[] array2 = list2.toArray(new String[list2.size()]);

  // -------------------------------------------------------------------------
  // 1) The assertion after this comment line DOES NOT COMPILE
  // Assert.assertThat(array2, IsIterableContainingInOrder.contains(array1));
  // -------------------------------------------------------------------------
  // 2) The assertion after this comment line DOES NOT COMPILE
  // Assert.assertThat(list2, IsIterableContainingInOrder.contains(list1));
  // -------------------------------------------------------------------------
  // 3) The assertion after this comment line SUCCEEDS
  Assert.assertThat(list2, IsIterableContainingInOrder.contains(array1));
  // -------------------------------------------------------------------------
  // 4) The assertion after this comment line DOES NOT COMPILE + HAS WARNING
  // Assert.assertThat(array2, IsIterableContainingInOrder.contains(list1));
  // -------------------------------------------------------------------------
  // 5) The assertion after this comment line DOES NOT COMPILE
  // Assert.assertThat(array2, IsIterableContainingInOrder.contains(string1));
  // -------------------------------------------------------------------------
  // 6) The assertion after this comment line COMPILES but fails
  Assert.assertThat(list2, IsIterableContainingInOrder.contains(string1));
  // -------------------------------------------------------------------------
  // 7) The assertion after this comment line COMPILES and succeeds
  Assert.assertThat(list2,
      IsIterableContainingInOrder.contains(string1, string2, string3));
  // -------------------------------------------------------------------------
}

大量的惊讶。 :(


PS 我意识到所有的惊讶都来自于我自己的无知,而不是别的。我应该复习泛型、类型推断、可变参数等。我真的可以对此进行详尽的解释,并且我可能会在以后多次回顾它。

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)));
}

最佳答案

这是答案...希望是正确的:)

// 1) The assertion after this comment line DOES NOT COMPILE
// Assert.assertThat(array2, IsIterableContainingInOrder.contains(array1));

这个不能编译,因为数组没有实现可迭代。

// 2) The assertion after this comment line DOES NOT COMPILE
// Assert.assertThat(list2, IsIterableContainingInOrder.contains(list1));

为此,list2 应该是 List<List<String>> , as contains 仅验证可迭代对象是否包含传递的项目

// 3) The assertion after this comment line SUCCEEDS
Assert.assertThat(list2, IsIterableContainingInOrder.contains(array1));

通过,因为 array1 被当作可变参数。

// 4) & 5) 

同1

// 6) The assertion after this comment line COMPILES but fails
Assert.assertThat(list2, IsIterableContainingInOrder.contains(string1));

包含集合中的所有项,而不仅仅是子集。

// 7)

与 3 类似,但每个项目都通过了。

关于java - 为什么 IsIterableContainingInOrder Hamcrest 匹配器无法处理数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12500973/

相关文章:

java - hamcrest 中的严格匹配?

junit - 在 JUnit 中将 "assertTrue"重写为 "assertThat"?

java - Spring MVC 将 406 返回到 Controller 测试

java - JUnit - 一次在@Before 中启动的字段在测试中为空

java - 在 FutureCallback 中启动 ListenableFuture 并在所有完成后回调

java - 套接字 : could not connect to server or specific port

java - JUnit 测试的 Maven 代码覆盖率报告

java - 为什么在 Java 泛型中返回类型是 `<T>Type` 而不是 `Type<T>` ?

java - 为什么以下正则表达式 [^0-9!a-zA-z#\\$% &'\\*\\+\\-/=\\?\\^_`\\{\\|\\}~@\\.]+ 对于 String.split 不起作用?

java - Windows XP 防火墙阻止入站 UDP 数据包 : programmatic exception for Java program or workaround?