简体   繁体   English

巨星断言顺序

[英]fest asserting order in list

I would like to test if the elements in a list are in a particular order. 我想测试列表中的元素是否按特定顺序排列。 Specifically I would like to test for a member of the elements. 具体来说,我想测试元素的成员。 So something like: 所以像这样:

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

Is is possible to do something like this ? 有可能做这样的事情吗? Right now I manually iterate over the elements and have an assertion for each one. 现在,我手动遍历元素,并对每个元素都有一个断言。

Having a quick browse though the version 1 source code ( link here ) I found this method which claims to be doing what you want: 快速浏览了版本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));
}

If I'm understanding the Javadoc correctly, you would just do this: 如果我正确理解Javadoc,则可以执行以下操作:

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

Note though that there were some issues with this method in version 2.0-M8. 请注意,尽管此方法在2.0-M8版本中存在一些问题。 As detailed here! 如此处详细! They were resolved in version 2.0-M9. 它们已在2.0-M9版本中解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM