From b44787ed31016343844878e5339a22bb6a1fe2ab Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 1 Feb 2022 09:10:52 +0100 Subject: [PATCH] Turn checkChoose into Parameterized Test --- src/main/java/Player.java | 11 +++++++++-- src/test/java/PlayerTest.java | 25 ++++++++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/Player.java b/src/main/java/Player.java index 091eb59..242bb02 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -57,7 +57,14 @@ public class Player { public int choose() { scanner = new Scanner(System.in); System.out.println("Eingabe:"); - int input = scanner.nextInt(); - return input; + try{ + int input = scanner.nextInt(); + if (input > 4 || input < 1) { + return -1; + } + return input; + } catch (Exception e) { + return -1; + } } } diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index d63631d..62ced73 100644 --- a/src/test/java/PlayerTest.java +++ b/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 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) + ); } }