Browse Source

Refactoring MatchfieldShipTest class: added description for tests and outsourced multiple tests into more functions

fleetstorm
Lorenz Hohmann 3 years ago
parent
commit
aae87e50e1
  1. 29
      src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java

29
src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java

@ -1,6 +1,7 @@
package de.tims.fleetstorm.matchfield; package de.tims.fleetstorm.matchfield;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -28,7 +29,7 @@ class MatchfieldShipTest {
shipSetCounter++; shipSetCounter++;
} }
assertTrue(shipSetCounter == length);
assertThat(shipSetCounter).describedAs(testName).isEqualTo(length);
} }
static Stream<Arguments> testShipPositioning() { static Stream<Arguments> testShipPositioning() {
@ -57,10 +58,10 @@ class MatchfieldShipTest {
boolean calculatedResult = matchfield.setShip(coordinate, length, direction); boolean calculatedResult = matchfield.setShip(coordinate, length, direction);
if (expectedResult) if (expectedResult)
assertTrue(calculatedResult);
assertThat(calculatedResult).describedAs(testName).isEqualTo(true);
if (!expectedResult) if (!expectedResult)
assertFalse(calculatedResult);
assertThat(calculatedResult).describedAs(testName).isEqualTo(false);
} }
static Stream<Arguments> testShipPositioningFailed() { static Stream<Arguments> testShipPositioningFailed() {
@ -77,28 +78,38 @@ class MatchfieldShipTest {
matchfield.createMatchfield(); matchfield.createMatchfield();
matchfield.setShip(new Coordinate(0, 0), 5, 1); 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 @Test
void testIfAllShipsOnMatchfieldHit() {
void testIfAllShipsHitReturnsFalseWhenNoShipIsHit() {
Matchfield matchfield = new Matchfield(10); Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield(); matchfield.createMatchfield();
matchfield.setShip(new Coordinate(0, 0), 5, 1); matchfield.setShip(new Coordinate(0, 0), 5, 1);
boolean calulatedResult = matchfield.areAllShipsHit(); boolean calulatedResult = matchfield.areAllShipsHit();
assertFalse(calulatedResult); 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, 0).setState(Coordinate.HIT);
matchfield.getField(0, 1).setState(Coordinate.HIT); matchfield.getField(0, 1).setState(Coordinate.HIT);
matchfield.getField(0, 2).setState(Coordinate.HIT); matchfield.getField(0, 2).setState(Coordinate.HIT);
matchfield.getField(0, 3).setState(Coordinate.HIT); matchfield.getField(0, 3).setState(Coordinate.HIT);
matchfield.getField(0, 4).setState(Coordinate.HIT); matchfield.getField(0, 4).setState(Coordinate.HIT);
calulatedResult = matchfield.areAllShipsHit();
boolean calulatedResult = matchfield.areAllShipsHit();
assertTrue(calulatedResult); assertTrue(calulatedResult);
} }
} }
Loading…
Cancel
Save