From 0106ea257708fffe635c0dd00b9e906a160f3730 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 14:41:15 +0100 Subject: [PATCH] Refactoring: Use ParameterizedTest in CoordinateTest --- .../fleetstorm/matchfield/Coordinate.java | 1 + .../fleetstorm/matchfield/CoordinateTest.java | 34 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index 2f7ec5a..3e16694 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -31,4 +31,5 @@ public class Coordinate { public void setState(int state) { this.state = state; } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index f7bb7f1..488aeda 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -1,8 +1,14 @@ package de.tims.fleetstorm.matchfield; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.stream.Stream; + import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; class CoordinateTest { @@ -23,22 +29,18 @@ class CoordinateTest { assertEquals(coordinate.getState(), 0); } - @Test - void testGetAndSetForAllStates() { - coordinate.setState(Coordinate.SHIP); - assertEquals(coordinate.getState(), Coordinate.SHIP); - assertEquals(coordinate.getState(), 1); - - coordinate.setState(Coordinate.SHOT); - assertEquals(coordinate.getState(), Coordinate.SHOT); - assertEquals(coordinate.getState(), 2); - - coordinate.setState(Coordinate.HIT); - assertEquals(coordinate.getState(), Coordinate.HIT); - assertEquals(coordinate.getState(), 3); + @ParameterizedTest(name = "All Getters/Setters are working") + @MethodSource("AllGettersAreWorking") + void testGetAndSetForAllStates(String testName, int state, int stateCode, int expectedResult) { + coordinate.setState(state); + int result = coordinate.getState(); + assertThat(result).describedAs(testName).isEqualTo(expectedResult); + } - coordinate.setState(Coordinate.EMPTY); - assertEquals(coordinate.getState(), Coordinate.EMPTY); - assertEquals(coordinate.getState(), 0); + static Stream AllGettersAreWorking() { + return Stream.of(Arguments.of("State = SHIP", 1, Coordinate.SHIP, Coordinate.SHIP), + Arguments.of("State = SHOT", 2, Coordinate.SHOT, Coordinate.SHOT), + Arguments.of("State = HIT", 3, Coordinate.HIT, Coordinate.HIT), + Arguments.of("State = EMPTY", 0, Coordinate.EMPTY, Coordinate.EMPTY)); } }