From 78dbaf9b3998bdb41ebe95aa7880b02c2f241b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lino?= Date: Tue, 2 Aug 2022 19:48:44 +0200 Subject: [PATCH] remove fails, use asserts --- .../scoreboard/reference/TestScoreboard.java | 48 ++++++------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/src/test/java/org/example/scoreboard/reference/TestScoreboard.java b/src/test/java/org/example/scoreboard/reference/TestScoreboard.java index 76b09f6..5aff942 100644 --- a/src/test/java/org/example/scoreboard/reference/TestScoreboard.java +++ b/src/test/java/org/example/scoreboard/reference/TestScoreboard.java @@ -7,7 +7,6 @@ import org.junit.Before; import org.junit.Test; import java.util.*; -import java.util.stream.Collectors; import java.util.stream.Stream; import static org.junit.Assert.*; @@ -70,45 +69,35 @@ public class TestScoreboard { @Test public void shouldFinishAGame_whenGameHasBeenStarted() { IMatch match = scoreboard.startMatch(Mexico, Canada); - if (match != scoreboard.finishMatch(match)) { - fail("unable to finish game"); - } + assertEquals(match, scoreboard.finishMatch(match)); } // should throw NoSuchElementException match is not ongoing @Test public void shouldThrowNoSuchElementException_whenMatchNotFound() { Match match = new Match(Mexico, Canada); - try { - scoreboard.finishMatch(match); - fail("exception was not thrown"); - } catch (NoSuchElementException ignored) {} + assertThrows(NoSuchElementException.class, () -> scoreboard.finishMatch(match)); } // summary should be empty when no match has started @Test public void shouldBeEmptySummary_whenNoMatchHasStarted() { - if(!scoreboard.getGameSummary().isEmpty()) { - fail("Summary was not empty on creation."); - } + assertTrue(scoreboard.getGameSummary().isEmpty()); } // should get game in summary when the game has been started before @Test public void shouldGetGameInSummary_whenGameHasBeenStarted() { IMatch match = scoreboard.startMatch(Mexico, Canada); - if (match != scoreboard.getGameSummary().get(0)) { - fail("unable to get single ongoing game"); - } + assertEquals(match, scoreboard.getGameSummary().get(0)); } // score for a match that just started should be 0-0 @Test public void scoreShouldBeZeroZero_whenMatchJustStarted() { IMatch match = scoreboard.startMatch(Mexico, Canada); - if (match.getHomeTeamScore().getGoals() != 0 || match.getAwayTeamScore().getGoals() != 0) { - fail("initial score was not 0-0"); - } + assertEquals(Integer.valueOf(0), match.getHomeTeamScore().getGoals()); + assertEquals(Integer.valueOf(0), match.getAwayTeamScore().getGoals()); } // score increase for home team when home team score is updated to + 1 @@ -117,9 +106,8 @@ public class TestScoreboard { IMatch match = scoreboard.startMatch(Mexico, Canada); IScore updatedHomeTeamScore = new Score(match.getHomeTeamScore().getGoals() + 1); scoreboard.updateScore(match, updatedHomeTeamScore, match.getAwayTeamScore()); - if (match.getHomeTeamScore().getGoals() != 1 || match.getAwayTeamScore().getGoals() != 0) { - fail("updated score was not 1-0"); - } + assertEquals(Integer.valueOf(1), match.getHomeTeamScore().getGoals()); + assertEquals(Integer.valueOf(0), match.getAwayTeamScore().getGoals()); } // should present matches ordered from newest to oldest when summary is requested @@ -136,13 +124,9 @@ public class TestScoreboard { IMatch match5 = scoreboard.startMatch(Argentina, Australia); match5 = scoreboard.updateScore(match5, new Score(5), new Score(5)); - ArrayList expectedResult = Stream.of(match5, match4, match3, match2, match1).collect(Collectors.toCollection(ArrayList::new)); - ArrayList gameSummary = scoreboard.getGameSummary(); - - if (!expectedResult.equals(gameSummary)) { - gameSummary.forEach(System.out::println); - fail("failed to return expected ordered summary"); - } + Object[] expectedResult = Stream.of(match5, match4, match3, match2, match1).toArray(); + Object[] gameSummary = scoreboard.getGameSummary().toArray(); + assertArrayEquals(expectedResult, gameSummary); } // should present matches ordered from highest combined score to lowest, newest to oldest on collision, when summary is requested @@ -159,12 +143,8 @@ public class TestScoreboard { IMatch match5 = scoreboard.startMatch(Argentina, Australia); match5 = scoreboard.updateScore(match5, new Score(3), new Score(1)); - ArrayList expectedResult = Stream.of(match4, match2, match1, match5, match3).collect(Collectors.toCollection(ArrayList::new)); - ArrayList gameSummary = scoreboard.getGameSummary(); - - if (!expectedResult.equals(gameSummary)) { - gameSummary.forEach(System.out::println); - fail("failed to return expected ordered summary"); - } + Object[] expectedResult = Stream.of(match4, match2, match1, match5, match3).toArray(); + Object[] gameSummary = scoreboard.getGameSummary().toArray(); + assertArrayEquals(expectedResult, gameSummary); } } -- 2.24.1