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;
}
2条答案
按热度按时间yqkkidmi1#
正如Annamalai的回答中提到的,自定义Assert是实现所需API的方法。
但是,如果可以选择依赖现有的AssertJ API,下面是一个可能的
anySatisfy
解决方案:ru9i0ody2#
下面的类可能会解决您的问题。
比赛Assert
工作组Assert
播放器Assert
如果需要,您也可以在TournamentAssert中使用单个函数进行Assert: