我用的是AssertJ。我有一个MyObj这样的类。我有一个MyObj的List。
AssertJ
MyObj
List
class MyObj { ... Map<K,V> myMap; ... }
当我用途:
assertThat(list).extracting("myMap")
.containsKey()
assertThat(list).extracting("myMap", Map.class)
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
uujelgoq2#
AssertMap内容的最简单方法是链接extracting方法:
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");
np8igboo3#
提取特征记录如下:http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#extracted-properties-assertion您在assertj-examples中有可执行的示例,特别是在IterableAssertionsExamples中。希望能有所帮助!
x759pob24#
如果你想使用containsKey方法,你必须在编译时知道键的类型(也就是说,你不能依赖于泛型)。
containsKey
assertThat(list) .extracting("myMap") .asInstanceOf(InstanceOfAssertFactories.map(String.class, Long.class)) .containsKey("key");
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); } } }
ef1yzkbh6#
一种方法是从List中提取Map并验证其内容,如这里所建议的-Assertj Core Features,如下所示:
Map
@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; }
6条答案
按热度按时间tgabmvqs1#
AssertJ有entry()方法。你可以像这样AssertMap值。
下面是javadoc:http://joel-costigliola.github.io/assertj/core/api/org/assertj/core/data/MapEntry.html
uujelgoq2#
AssertMap内容的最简单方法是链接
extracting
方法:np8igboo3#
提取特征记录如下:http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#extracted-properties-assertion
您在assertj-examples中有可执行的示例,特别是在IterableAssertionsExamples中。
希望能有所帮助!
x759pob24#
如果你想使用
containsKey
方法,你必须在编译时知道键的类型(也就是说,你不能依赖于泛型)。w46czmvw5#
这有点复杂,但绝对可行:
ef1yzkbh6#
一种方法是从
List
中提取Map
并验证其内容,如这里所建议的-Assertj Core Features,如下所示: