fest在列表中断言顺序

时间:2014-10-26 10:29:12

标签: java unit-testing junit fest

我想测试列表中的元素是否按特定顺序排列。具体来说,我想测试一个元素的成员。如下所示:

assertThat(listOfObjects).hasProperty(name).inOrder("one", "two", "three");

有可能做这样的事吗?现在我手动迭代元素并为每个元素进行断言。

1 个答案:

答案 0 :(得分:0)

快速浏览版本1源代码(link here)后,我发现这种方法声称正在做你想做的事情:

/**
 * Verifies that the actual {@code List} contains the given objects, in the same order. This method works just like
 * {@code isEqualTo(List)}, with the difference that internally the given array is converted to a {@code List}.
 *
 * @param objects the objects to look for.
 * @return this assertion object.
 * @throws AssertionError       if the actual {@code List} is {@code null}.
 * @throws NullPointerException if the given array is {@code null}.
 * @throws AssertionError       if the actual {@code List} does not contain the given objects.
 */
public @Nonnull ListAssert containsExactly(@Nonnull Object... objects) {
  checkNotNull(objects);
  return isNotNull().isEqualTo(newArrayList(objects));
}

如果我正确理解Javadoc,你就会这样做:

assertThat(listOfObjects).containsExactly(object1, object2, object3);

请注意,在2.0-M8版本中,此方法存在一些问题。 As detailed here!它们已在版本2.0-M9中得到解决。