The .contains(...) collection uses the equals and hashCode methods of objects. To use equals (or in this case contains ) in your own objects, you need to override the equals and hashCode methods of your class. This is because Java uses links behind the scenes, so although the field may be the same, object references are not.
In Eclipse, you can generate them using right-mouse click Source Generate hashCode() and equals()... But, since you never claimed to use Eclipse, here is an example of the methods that are generated:
// Overriding this class' equals and hashCode methods for Object comparing purposes // using the Collection contains // contains does the following behind the scenes: Check if both inputs aren't null, // check if the HashCodes match, check if the Objects are equal. // Therefore to use the Collection contains for Objects with the same fields, we // need to override the Object equals and hashCode methods // These methods below are generated by Eclipse itself using "Source -> Generate // hashCode() and equals()..." @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null) return false; if(getClass() != obj.getClass()) return false; Item other = (Item) obj; if(name == null){ if(other.name != null) return false; } else if(!name.equals(other.name)) return false; return true; }
If you add both of them to the Item class, contains will work.
EDIT:
I'm not sure, but when I look at your code, I think the following may be wrong:
@Test public void getAllItems() { Collection<Item> actualItems = auction.getAllItems(joe); Collection<Item> expectedItems = Lists.newArrayList();
If you try the following:
@Test public void getAllItems() { Collection<Item> actualItems = auction.getAllItems(joe); Collection<Item> expectedItems = Lists.newArrayList();
Is a list of 4 items included in the waitlist?
[Item{name=iPhone}, Item{name=Skateboard}, Item{name=iPhone}, Item{name=Skateboard}] --> Expected [Item{name=iPhone}, Item{name=Skateboard}] --> Actual
In this case, you should not add two elements, since they are already in the list.
In addition, you are trying to use contains for the entire list. Usually contains used to see if a single item is present in a list. So you can use something like this:
for(Item i : expectedList){ assertTrue(actualList.contains(i)); }
or maybe something like this if you use these libraries :
assertThat(actualList, is(expectedList));
I'm not sure if this is the reason, and if it is fixed, since you are using a different JUnit library, then I usually do, and I'm not sure if this syntax is possible with Asserts.