Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use assertj extracting map property

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?

like image 469
lhoak Avatar asked Jul 25 '15 08:07

lhoak


People also ask

Should I use AssertJ?

Using AssertJ you can write cleaner and more readable code. Plus it is super easy to learn and use. AssertJ works with any Java version over Java 6, it also supports assertions using the newer features in Java such as lambdas. Find more about methods in AssertJ here.

What is recursive comparison?

Strict/lenient recursive comparison Compatible means that the expected object/field types are the same or a subtype of actual/field types, for example if actual is an Animal and expected a Dog , they will be compared fiels by field in strict type checking mode.

What is AssertJ in Java?

AssertJ core is a Java library that provides a fluent interface for writing assertions. Its main goal is to improve test code readability and make maintenance of tests easier. AssertJ core provides assertions for JDK standard types and can be used with JUnit, TestNG or any other test framework.


1 Answers

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

like image 170
Min Hyoung Hong Avatar answered Oct 05 '22 23:10

Min Hyoung Hong