使用 AssertJ 检查单元测试中 Map 条目内的列表

Check a list inside a Map entry in a unit test using AssertJ

我想使用 AssertJ 库在单元测试中检查 Map 值内的列表:

public class Group {
    List<Player> players = new ArrayList<>();

    public Group(List<Player> players) {
        this.players.addAll(players);
    }
}
Map<Character, Group> generatedGroups = receiveFromAnyMethod();

在地图中我有这个:

A: Group A -> players(playerA, playerB)

如何查看组内的列表?我想我应该使用提取,flatExtracting 方法,但我不知道如何。

for (Map.Entry<Character, Group> entry : generatedGroups.entrySet()) {
    
    List<Player> players = entry.getValue().getPlayers();
    // Do your check here
}

假设character为输入键,player1player2为内表期望内容,可以这样写:

assertThat(generatedGroups.get(character))
  .extracting("players", as(InstanceOfAssertFactories.LIST))
  .containsExactly(player1, player2);

或具有类型安全性,以防 Group 提供 getPlayers():

assertThat(generatedGroups.get(character))
  .extracting(Group::getPlayers, as(InstanceOfAssertFactories.LIST))
  .containsExactly(player1, player2);

参考: