junit 使用assertj测试列表中是否至少有一个元素包含特定元素

hec6srdp  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(160)

假设我有球员组成一个团队参加锦标赛,我想测试,锦标赛至少有一个团队的名字为T1的球员名为保罗。
第一个
测试应如下所示:

Tournament tournament = new Tournament();
tournament.create();
assertThat(tournament)
  .hasAtLeastOneTeam()
  .withName("T1")
  .withAPlayer()
  .named("Paul")

如何使用AssertJ实现这一点?

yqkkidmi

yqkkidmi1#

正如Annamalai的回答中提到的,自定义Assert是实现所需API的方法。
但是,如果可以选择依赖现有的AssertJ API,下面是一个可能的anySatisfy解决方案:

assertThat(tournament.getTeams()).anySatisfy(team -> {
  assertThat(team.getName()).isEqualTo("T1");
  assertThat(team.getPlayers()).extracting(Player::getFirstName).contains("Paul");
});
ru9i0ody

ru9i0ody2#

下面的类可能会解决您的问题。

比赛Assert

public class TournamentAssert extends AbstractAssert<TournamentAssert, Tournament> {
    public TournamentAssert(Tournament actual) {
        super(actual, TournamentAssert.class);
    }
    public static TournamentAssert assertThat(Tournament actual) {
        return new TournamentAssert(actual);
    }
    public TournamentAssert hasAtLeastOneTeam() {
        isNotNull();
        if (actual.getTeams().size() > 0) {
            failWithMessage("Expected at least one team But actual size is: <%s>", actual.getTeams().size());
        }
        return this;
    }
    public TeamAssert withName(String name) {
        isNotNull();
        for(Team team: actual.getTeams()) {
            if(name.equals(team.getName())) {
                return TeamAssert.assertThat(team);
            }
        }
        return TeamAssert.assertThat(null);
    }
}

工作组Assert

public class TeamAssert extends AbstractAssert<TeamAssert, Team> {
    public TeamAssert(Team actual) {
        super(actual, TeamAssert.class);
    }
    public static TeamAssert assertThat(Team actual) {
        return new TeamAssert(actual);
    }
    public PlayerAssert withAPlayer() {
        isNotNull();
        if (actual.getPlayers().size() == 1) {
            for(Player player: actual.getPlayers()) {
                return PlayerAssert.assertThat(player);
            }
        }
        return PlayerAssert.assertThat(null);
    }
}

播放器Assert

public class PlayerAssert extends AbstractAssert<PlayerAssert, Player> {
    public PlayerAssert(Player actual) {
        super(actual, PlayerAssert.class);
    }
    public static PlayerAssert assertThat(Player actual) {
        return new PlayerAssert(actual);
    }
    public PlayerAssert named(String name) {
        isNotNull();
        if (!name.equals(actual.getFirstName())) {
            failWithMessage("Expected a player name is: <%s> But actual name is <%s>",name,  actual.getFirstName());
        }
        return this;
    }
}

如果需要,您也可以在TournamentAssert中使用单个函数进行Assert:

public TeamAssert withNameAndAPlayerAndNamed(String name, String playerName) {
            isNotNull();
            for(Team team: actual.getTeams()) {
                if(name.equals(team.getName())) {
                    if (team.getPlayers().size() == 1) {
                        for(Player player: team.getPlayers()) {
                            if (!playerName.equals(player.getFirstName())) {
                                failWithMessage("Expected a player name is: <%s> But actual name is <%s>",name,  player.getFirstName());
                            }
                        }
                    }
                }
            }
            return this;
        }

相关问题