0

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

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

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

4

1 回答 1

0

快速浏览第 1 版源代码(此处的链接)我发现这种方法声称正在做你想做的事:

/**
 * 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 版本中,此方法存在一些问题。详细到这里!它们在 2.0-M9 版本中得到解决。

于 2014-10-26T10:35:22.330 回答