【发布时间】:2014-10-26 10:29:12
【问题描述】:
我想测试列表中的元素是否按特定顺序排列。具体来说,我想测试元素的成员。所以像:
assertThat(listOfObjects).hasProperty(name).inOrder("one", "two", "three");
有可能做这样的事情吗?现在我手动迭代元素并为每个元素设置一个断言。
【问题讨论】:
标签: java unit-testing junit fest
我想测试列表中的元素是否按特定顺序排列。具体来说,我想测试元素的成员。所以像:
assertThat(listOfObjects).hasProperty(name).inOrder("one", "two", "three");
有可能做这样的事情吗?现在我手动迭代元素并为每个元素设置一个断言。
【问题讨论】:
标签: java unit-testing junit fest
快速浏览版本 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 版本已解决。
【讨论】:
name 的属性,它具有这些值。