how to use assertj extracting map property

lhoak picture lhoak · Jul 25, 2015 · Viewed 12.5k times · Source

I am using AssertJ. I have a class like MyObj. And I have a List of MyObj.

Class MyObj {
    ...
    Map<K,V> myMap;
    ...
}

When I use:

  1. assertThat(list).extracting("myMap"), I cannot use .containsKey() method.
  2. I also tried using assertThat(list).extracting("myMap", Map.class), but it does not work either.

What is the right way of using it?

Answer

Min Hyoung Hong picture Min Hyoung Hong · Feb 27, 2019

AssertJ has entry() method. You can assert map value like this.

assertThat(list)
    .extracting("myMap")
    .contains(entry("foo1", "bar1"), entry("foo2", "bar2"));

Here's javadoc : http://joel-costigliola.github.io/assertj/core/api/org/assertj/core/data/MapEntry.html