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" }
In other words, a MyObject
both remembers and exposes the list of parameters that brought it into existence. More details about this contract:
getComponents
will be the same as the one chosen for object creationnull
is undefined (other code guarantees no null
gets to the factory)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.
Here my attempt:
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()
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?Why not simply use List#equals
?
assertEquals(argumentComponents, imapPathComponents);
two lists are defined to be equal if they contain the same elements in the same order.