我想测试列表中的元素是否按特定顺序排列。具体来说,我想测试一个元素的成员。所以类似于:
assertThat(listOfObjects).hasProperty(name).inOrder("one", "two", "three");
有可能做这样的事情吗?现在,我手动迭代这些元素,并为每个元素都有一个断言。
快速浏览版本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中解决了这些问题。