AssertJ:调用提取后的FlatMap列表

时间:2017-07-13 13:06:56

标签: java java-8 assertj

所以我有一个字符串/字符串列表对,我想要做的是提取后,将返回的列表合并到一个列表中,我可以执行更多的断言:

MyTest.java

Map<String, List<String>> testMap  = new HashMap<>();
List<String> nameList = newArrayList("Dave", "Jeff");
List<String> jobList = newArrayList("Plumber", "Builder");
List<String> cityList = newArrayList("Dover", "Boston");

testMap.put("name", nameList);
testMap.put("job", jobList);
testMap.put("city", cityList);

assertThat(testMap).hasSize(3)
    .extracting("name", "city")
    //not sure where to go from here to flatten list of lists
    // I want to do something like .flatMap().contains(expectedValuesList)

当我调用extract时,它会将列表值拉出到列表列表中,这很好,但我之后不能调用flatExtracting,因为没有要传入的属性名称,而且我从中读过它似乎不适合自定义提取器(或者我不完全确定如何将它放在一起)。是否还有另一种方法可以平息我回来的列表清单?我可以使用稍长的路径并在列表列表上执行断言,或者在断言之前使用lambda来收集结果但是我想将断言保持为一个(例如,一些映射断言然后链接一些断言)内容)

1 个答案:

答案 0 :(得分:7)

flatExtracting不在地图断言API(尚未)中,您可以改为:

assertThat(testMap)
        .hasSize(3)
        .extracting("name", "city", "job")
        .flatExtracting(list -> ((List<String>) list))
        .contains("Dave", "Jeff", "Plumber", "Builder", "Dover", "Boston");

我结束了创建https://github.com/joel-costigliola/assertj-core/issues/1034以支持此用例