junit 如何使用assertj提取Map属性

lxkprmvk  于 2022-11-11  发布在  其他
关注(0)|答案(6)|浏览(141)

我用的是AssertJ。我有一个MyObj这样的类。我有一个MyObjList

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

当我用途:

  1. assertThat(list).extracting("myMap"),我无法使用.containsKey()方法。
    1.我也尝试使用assertThat(list).extracting("myMap", Map.class),但它也不起作用。
    正确的使用方法是什么?
tgabmvqs

tgabmvqs1#

AssertJ有entry()方法。你可以像这样AssertMap值。

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

uujelgoq

uujelgoq2#

AssertMap内容的最简单方法是链接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");
np8igboo

np8igboo3#

提取特征记录如下:http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#extracted-properties-assertion
您在assertj-examples中有可执行的示例,特别是在IterableAssertionsExamples中。
希望能有所帮助!

x759pob2

x759pob24#

如果你想使用containsKey方法,你必须在编译时知道键的类型(也就是说,你不能依赖于泛型)。

assertThat(list)
  .extracting("myMap")
  .asInstanceOf(InstanceOfAssertFactories.map(String.class, Long.class))
  .containsKey("key");
w46czmvw

w46czmvw5#

这有点复杂,但绝对可行

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);
        }
    }
}
ef1yzkbh

ef1yzkbh6#

一种方法是从List中提取Map并验证其内容,如这里所建议的-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;
}

相关问题