Browse Source

Refactoring: Use ParameterizedTest in CoordinateTest

fleetstorm
Max Wenzel 3 years ago
committed by Lorenz Hohmann
parent
commit
0106ea2577
  1. 1
      src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java
  2. 34
      src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java

1
src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java

@ -31,4 +31,5 @@ public class Coordinate {
public void setState(int state) {
this.state = state;
}
}

34
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<Arguments> 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));
}
}
Loading…
Cancel
Save