How to perform deeper matching of keys and values with assertj
You can also do something like this:
assertThat(characterAges).contains(entry("Frodo", 34), ...);
See https://github.com/joel-costigliola/assertj-core/wiki/New-and-noteworthy#new-map-assertions
There is no easy solution for this. One way is to implement a custom Assertion for the character map. Here is a simple custom Assertion example for this problem:
public class CharacterMapAssert extends AbstractMapAssert<MapAssert<Character, Integer>, Map<Character, Integer>, Character, Integer> {
public CharacterMapAssert(Map<Character, Integer> actual) {
super(actual, CharacterMapAssert.class);
}
public static CharacterMapAssert assertThat(Map<Character, Integer> actual) {
return new CharacterMapAssert(actual);
}
public CharacterMapAssert hasNameWithAge(String name, int age) {
isNotNull();
for (Map.Entry<Character, Integer> entrySet : actual.entrySet()) {
if (entrySet.getKey().getName().contains(name) && (int) entrySet.getValue() == age) {
return this;
}
}
String msg = String.format("entry with name %s and age %s does not exist", name, age);
throw new AssertionError(msg);
}
}
In the test case:
assertThat(characterAges).hasNameWithAge("Frodo", 34);
Be aware that you have for every custom data structure to write your own assertion. For you Character
class you can generate a assertion with the AssertJ assertions generator.
Update Java 8
With Java 8 can also the Lambda Expression used
assertThat(characterAges).matches(
(Map<Character, Integer> t)
-> t.entrySet().stream().anyMatch((Map.Entry<Character, Integer> t1)
-> "Frodo".equals(t1.getKey().getName()) && 34 == t1.getValue()),
"is Frodo and 34 years old"
);