我正在为 MyObject类编写一个简单的 < a href = “ http://www.JUnit.org/”rel = “ noReferrer”title = “ JUnit: 用于测试驱动开发的资源”> JUnit 测试。
MyObject可以从采用 一个 href = “ http://docs.oracle.com/javase/7/docs/api/java/lang/String.html”rel = “ noReferrer”title = “ JDK: java.lang. String”> String 变量的静态工厂方法创建。
MyObject.ofComponents("Uno", "Dos", "Tres");
在 MyObject存在期间的任何时候,客户端都可以通过 .getComponents()方法以 网址: http://docs.oracle.com/javase/7/docs/api/java/util/List.html的形式检查它创建的参数。
myObject.ofComponents(); // -> List<String>: { "Uno", "Dos", "Tres" }
换句话说,MyObject既记住又公开使其存在的参数列表。关于这份合同的更多细节:
getComponents的顺序将与为创建对象选择的顺序相同null上的行为是未定义的(其他代码保证 null不会到达工厂)我正在编写一个简单的测试,从 一个 href = “ http://docs.oracle.com/javase/7/docs/api/java/lang/String.html”rel = “ noReferrer”title = “ JDK: java.lang. String”> String 的列表中创建一个 MyObject,并检查它是否可以通过 .getComponents()返回相同的列表。我立即这样做,但这应该发生在一个现实的代码路径的距离.
这是我的尝试:
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));
Iterables.elementsEqual()吗?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?.toArray()是将 网址: http://docs.oracle.com/javase/7/docs/api/java/util/List.html转换为 E 变量的最佳方法吗?