How do I assert that two HashMap with Javabean values are equal?
If your Question class implements equals
then you can just do
assertEquals(expectedMap, hashMap);
assertTrue(expectedMap.equals(hashMap));
The Map interface specifies that two Maps are equal if they contain equal elements for equal keys.
Too compare maps ,in your particular case :
1)Check the size of the map if its equal
Then use
`assertTrue(expectedMap.equals(hashMap));`
In your Question bean you have to override the equals and hashcode method.
Here is the solution I eventually ended up using which worked perfectly for unit testing purposes.
for(Map.Entry<Integer, Question> entry : questionMap.entrySet()) {
assertReflectionEquals(entry.getValue(), expectedQuestionMap.get(entry.getKey()), ReflectionComparatorMode.LENIENT_ORDER);
}
This involves invoking assertReflectionEquals()
from the unitils
package.
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-core</artifactId>
<version>3.3</version>
<scope>test</scope>
</dependency>
Using Guava you can do:
assertTrue(Maps.difference(expected, actual).areEqual());