5

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?

4

6 回答 6

10

AssertJ 有 entry() 方法。您可以像这样断言地图值。

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

这是javadoc:http: //joel-costigliola.github.io/assertj/core/api/org/assertj/core/data/MapEntry.html

于 2019-02-27T07:19:42.963 回答
4

断言地图内容的最简单方法是链接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");
于 2015-08-11T12:15:01.660 回答
2

此处记录了提取功能:http: //joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#extracted-properties-assertion

您在assertj-examples中有可执行的示例,特别是在 IterableAssertionsExamples中。

希望能帮助到你 !

于 2015-07-25T11:50:08.093 回答
1

如果要使用containsKey方法,必须在编译时就知道键的类型(即不能依赖泛型)。假设你myMap可以Map<String, Long>这样做:

assertThat(list)
  .extracting("myMap")
  .asInstanceOf(InstanceOfAssertFactories.map(String.class, Long.class))
  .containsKey("key");
于 2021-08-03T15:11:28.933 回答
0

这有点复杂,但绝对有可能

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);
        }
    }
}
于 2015-07-25T11:59:10.107 回答
0

一种方法是按照此处的建议提取MapList验证其内容 - Assertj Core Features,如下所示:

@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;
}
于 2017-03-18T15:17:09.177 回答