我想测试列表中的元素是否按特定顺序排列。具体地说,我想测试元素中的一个成员。所以就像这样:
assertThat(listOfObjects).hasProperty(name).inOrder("one", "two", "three");有没有可能做这样的事情?现在,我手动迭代元素,并对每个元素都有一个断言。
发布于 2014-10-26 18:35:22
快速浏览一下版本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中解决了这些问题。
https://stackoverflow.com/questions/26572023
复制相似问题