How to use the assertj extracting map property

I am using AssertJ

. I have a class like MyObj

. And I have List

of MyObj

.

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

      

When I use:

  • assertThat(list).extracting("myMap")

    , I cannot use the method .containsKey()

    .
  • I also tried to use assertThat(list).extracting("myMap", Map.class)

    but it doesn't work either.

What is the correct way to use it?

+3


source to share


4 answers


The highlight function is documented here: http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#extracted-properties-assertion

You have executable examples in assertj-examples , namely IterableAssertionsExamples .



Hope this helps!

+2


source


The easiest way to assert the content of your card is with a bind method extracting

:



    MyObj o1 = new MyObj();
    o1.getMyMap().put("foo", "Hello");
    o1.getMyMap().put("bar", "Bye");
    MyObj o2 = new MyObj();
    o2.getMyMap().put("foo", "Hola");
    o2.getMyMap().put("bar", "Adios");

    List<MyObj> myObjs = Arrays.asList(o1, o2);
    assertThat(myObjs).extracting("myMap").extracting("foo").contains("Hello", "Hola");
    assertThat(myObjs).extracting("myMap").extracting("bar").contains("Bye", "Adios");

      

+1


source


This is a little more complicated, but definitely possible :

public class Main {
    public static void main(String[] args) {
        MyObject<String, Integer> myObject1 = new MyObject<>("A", 1);
        MyObject<String, Integer> myObject2 = new MyObject<>("B", 2);
        MyObject<String, Integer> myObject3 = new MyObject<>("C", 3);

        List<MyObject<String, Integer>> myObjects = Arrays.asList(myObject1, myObject2, myObject3);

        assertThat(myObjects).extracting("myMap", Map.class).is(containingKey("A"), atIndex(0))
                                                            .is(containingKey("B"), atIndex(1))
                                                            .is(containingKey("C"), atIndex(2));
    }

    private static class MapContainsKeyCondition<K> extends Condition<Map> {
        private final K keyToContain;

        public MapContainsKeyCondition(K key) {
            this.keyToContain = key;
        }

        @Override
        public boolean matches(Map map) {
            return map.containsKey(keyToContain);
        }
    }

    private static <K> Condition<Map> containingKey(K key) {
        return new MapContainsKeyCondition<>(key);
    }

    public static class MyObject<K, V> {
        final Map<K, V> myMap;

        public MyObject(K key, V value) {
            this.myMap = Collections.singletonMap(key, value);
        }
    }
}

      

0


source


One way is to extract Map

from List

and validate its contents, as suggested here, Assertj Core Features , namely:

@Test
public void getMyObjList() {
    assertThat(list).isNotEmpty().extracting("myMap")
            .isNotEmpty().contains(geMap());
}

private Map<String, Integer> geMap() {
    final Map<String, Integer> map = new HashMap<>();
    map.put("A", 1);
    map.put("B", 2);
    return map;
}

      

0


source







All Articles