如何JUnit测试两个List< E>包含相同顺序的相同元素? [英] How to JUnit test that two List<E> contain the same elements in the same order?
问题描述
我正在编写一个简单的 JUnit 测试MyObject
类.
I am writing a simple JUnit test for the MyObject
class.
A MyObject
can be created from a static factory method that takes a varargs of String.
MyObject.ofComponents("Uno", "Dos", "Tres");
At any time during the existence of MyObject
, clients can inspect the parameters it was created by in the form of a List<E>, through the .getComponents()
method.
myObject.ofComponents(); // -> List<String>: { "Uno", "Dos", "Tres" }
换句话说,MyObject
既记住并公开了使之成为现实的参数列表.有关此合同的更多详细信息:
In other words, a MyObject
both remembers and exposes the list of parameters that brought it into existence. More details about this contract:
- The order of
getComponents
will be the same as the one chosen for object creation - Duplicate subsequent String components are allowed and retained in order
- Behaviour on
null
is undefined (other code guarantees nonull
gets to the factory) - There are no ways to alter the list of components after object instantiation
I am writing a simple test that creates a MyObject
from a list of String and checks that it can return the same list via .getComponents()
. I do this immediately but this is supposed to happen at a distance in a realistic code path.
这是我的尝试:
List<String> argumentComponents = Lists.newArrayList("One", "Two", "Three");
List<String> returnedComponents =
MyObject.ofComponents(
argumentComponents.toArray(new String[argumentComponents.size()]))
.getComponents();
assertTrue(Iterables.elementsEqual(argumentComponents, returnedComponents));
问题
- 是 Google Guava
Iterables.elementsEqual()
最好的方法是,如果我在构建路径中有库,可以比较这两个列表?我一直在为此苦恼;我应该使用遍历
Question
- Is Google Guava
Iterables.elementsEqual()
the best way, provided I have the library in my build path, to compare those two lists? this is something I have been agonizing about; should I use this helper method which goes over an Iterable<E>.. check size and then iterate running.equals()
.. or any other of the methods that an Internet search suggests? what's the canonical way to compare lists for unit tests? - 方法测试是否合理设计?我不是 JUnit 的专家>!
-
.toArray()
是转换 推荐答案我更喜欢使用Hamcrest,因为在出现故障的情况下它可以提供更好的输出
I prefer using Hamcrest because it gives much better output in case of a failure
Assert.assertThat(listUnderTest, IsIterableContainingInOrder.contains(expectedList.toArray()));
代替报告
expected true, got false
它将报告
expected List containing "1, 2, 3, ..." got list containing "4, 6, 2, ..."
IsIterableContainingInOrder.contain
根据Javadoc:
为Iterables创建一个匹配器,该匹配器在检查的Iterable上单次通过时会产生一系列项目,每个项目在逻辑上都等于指定项目中的相应项目.对于正匹配,所检查的可迭代项的长度必须与指定项目的数量相同
Creates a matcher for Iterables that matches when a single pass over the examined Iterable yields a series of items, each logically equal to the corresponding item in the specified items. For a positive match, the examined iterable must be of the same length as the number of specified items
因此
listUnderTest
必须具有相同数量的元素,并且每个元素必须按顺序匹配期望值.So the
listUnderTest
must have the same number of elements and each element must match the expected values in order.这篇关于如何JUnit测试两个List< E>包含相同顺序的相同元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Is Google Guava