From aae87e50e163a6d9db0e7b5b5749ec35ec0f89d6 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 15:11:38 +0100 Subject: [PATCH] Refactoring MatchfieldShipTest class: added description for tests and outsourced multiple tests into more functions --- .../matchfield/MatchfieldShipTest.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index 0d08687..9948134 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -1,6 +1,7 @@ package de.tims.fleetstorm.matchfield; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -28,7 +29,7 @@ class MatchfieldShipTest { shipSetCounter++; } - assertTrue(shipSetCounter == length); + assertThat(shipSetCounter).describedAs(testName).isEqualTo(length); } static Stream testShipPositioning() { @@ -57,10 +58,10 @@ class MatchfieldShipTest { boolean calculatedResult = matchfield.setShip(coordinate, length, direction); if (expectedResult) - assertTrue(calculatedResult); + assertThat(calculatedResult).describedAs(testName).isEqualTo(true); if (!expectedResult) - assertFalse(calculatedResult); + assertThat(calculatedResult).describedAs(testName).isEqualTo(false); } static Stream testShipPositioningFailed() { @@ -77,28 +78,38 @@ class MatchfieldShipTest { matchfield.createMatchfield(); matchfield.setShip(new Coordinate(0, 0), 5, 1); - assertFalse(matchfield.setShip(new Coordinate(0, 1), 5, 1)); + assertThat(matchfield.setShip(new Coordinate(0, 1), 5, 1)).describedAs("set ship on coordinate with ship") + .isEqualTo(false); - assertTrue(matchfield.setShip(new Coordinate(1, 1), 4, 0)); - assertFalse(matchfield.setShip(new Coordinate(4, 1), 2, 1)); + assertThat(matchfield.setShip(new Coordinate(1, 1), 4, 0)).describedAs("set ship on coordinate without ship") + .isEqualTo(true); + assertThat(matchfield.setShip(new Coordinate(4, 1), 2, 1)) + .describedAs("set second ship on coordinate without ship").isEqualTo(false); } @Test - void testIfAllShipsOnMatchfieldHit() { + void testIfAllShipsHitReturnsFalseWhenNoShipIsHit() { Matchfield matchfield = new Matchfield(10); matchfield.createMatchfield(); matchfield.setShip(new Coordinate(0, 0), 5, 1); - boolean calulatedResult = matchfield.areAllShipsHit(); assertFalse(calulatedResult); + } + @Test + void testIfAllShipsHitReturnsTrueWhenAllShipsAreHit() { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + + matchfield.setShip(new Coordinate(0, 0), 5, 1); matchfield.getField(0, 0).setState(Coordinate.HIT); matchfield.getField(0, 1).setState(Coordinate.HIT); matchfield.getField(0, 2).setState(Coordinate.HIT); matchfield.getField(0, 3).setState(Coordinate.HIT); matchfield.getField(0, 4).setState(Coordinate.HIT); - calulatedResult = matchfield.areAllShipsHit(); + + boolean calulatedResult = matchfield.areAllShipsHit(); assertTrue(calulatedResult); } }