Browse Source

Turn checkChoose into Parameterized Test

AIPlayer
Jonas Wagner 3 years ago
parent
commit
b44787ed31
  1. 7
      src/main/java/Player.java
  2. 25
      src/test/java/PlayerTest.java

7
src/main/java/Player.java

@ -57,7 +57,14 @@ public class Player {
public int choose() {
scanner = new Scanner(System.in);
System.out.println("Eingabe:");
try{
int input = scanner.nextInt();
if (input > 4 || input < 1) {
return -1;
}
return input;
} catch (Exception e) {
return -1;
}
}
}

25
src/test/java/PlayerTest.java

@ -177,17 +177,28 @@ public class PlayerTest {
);
}
@Test
void checkChooseMethod() {
@ParameterizedTest
@MethodSource("checkChooseMethodData")
void checkChoose(String testName, String input, int expectedResult) {
InputStream stdin = System.in;
System.setIn(new ByteArrayInputStream("0\n".getBytes()));
System.setIn(new ByteArrayInputStream(input.getBytes()));
int calculatedResult = p.choose();
System.setIn(stdin);
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult);
}
int expectedResult = 0;
assertThat(calculatedResult).describedAs("Choose one Figure").isEqualTo(expectedResult);
static Stream<Arguments> checkChooseMethodData() {
return Stream.of(
Arguments.of("Figure 1 choosen", "1\n", 1),
Arguments.of("Figure 2 choosen", "2\n", 2),
Arguments.of("Figure 3 choosen", "3\n", 3),
Arguments.of("Figure 4 choosen", "4\n", 4),
Arguments.of("Index out of bounds choosen", "16\n", -1),
Arguments.of("Index out of bounds choosen", "-2\n", -1),
Arguments.of("Input a letter or char", "g\n", -1),
Arguments.of("Input a empty string", "\n", -1),
Arguments.of("Input a special character", "&\n", -1)
);
}
}
Loading…
Cancel
Save