From 5da1a41b2bb2251fe50da2b769c0aa3e9c40091a Mon Sep 17 00:00:00 2001 From: 0nlineSam Date: Thu, 9 Jan 2025 19:44:08 +0100 Subject: [PATCH] Task 1 b) 2 more test cases --- .../src/test/dicegame/logic/LogicTest.java | 55 ++++++++++++++----- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/dicegame/src/test/dicegame/logic/LogicTest.java b/dicegame/src/test/dicegame/logic/LogicTest.java index efbdf33b..efd992a6 100644 --- a/dicegame/src/test/dicegame/logic/LogicTest.java +++ b/dicegame/src/test/dicegame/logic/LogicTest.java @@ -5,6 +5,7 @@ import dicegame.logic.SelectionException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import org.mockito.stubbing.Answer; import java.util.Random; import java.util.function.Predicate; @@ -44,15 +45,12 @@ class LogicTest { logic = null; } - // Input a valid index: 0-5 + // K2 (c) + // Valid index: 0-5 // The value for this index is not 1 or 5 @Test void changeDiceStateWithValidDiceNot1Or5() { - Random mockedRandom = Mockito.mock(Random.class); - Mockito.when(mockedRandom.nextInt(6)).thenAnswer(invocation -> randomNumberNot0or4()); - logic = new Logic(mockedRandom); - logic.startGame(); - logic.rollDice(); + startGameAndRollDice(invocation -> randomNumberNot0or4()); Exception e = assertThrows(SelectionException.class, () -> logic.changeDiceState(0)); String expectedMessage = "Es können nur 1'en und 5'en fixiert werden!"; @@ -60,27 +58,56 @@ class LogicTest { assertEquals(expectedMessage, actualMessage); } + // K3 (a) + // Valid index: 0-5 + // Value: 0 | 5 + // Index refers to dice which has been fixed in previous round @Test void changeDiceStateWhenFixedInPrevRoll() { - Random mockedRandom = Mockito.mock(Random.class); - Mockito.when(mockedRandom.nextInt(6)).thenAnswer(invocation -> randomNumber0or4()); - logic = new Logic(mockedRandom); - logic.startGame(); - logic.rollDice(); + startGameAndRollDice(invocation -> randomNumber0or4()); assertDoesNotThrow(() -> logic.changeDiceState(0)); logic.rollDice(); assertThrows(Exception.class, () -> logic.changeDiceState(0)); } + // K3 (b) + // Valid index: 0-5 + // Value: 0 | 5 + // Index refers to dice which has been fixed in current round + @Test + void changeDiceStateWhenFixedInThisRoll() { + startGameAndRollDice(invocation -> randomNumber0or4()); + assertDoesNotThrow(() -> logic.changeDiceState(0)); + assertDoesNotThrow(() -> logic.changeDiceState(0)); + } + + // K3 (c) + // Valid index: 0-5 + // Value: 0 | 5 + // Index refers to dice which is not fixed + @Test + void changeDiceStateWhenNotFixed() { + startGameAndRollDice(invocation -> randomNumber0or4()); + assertDoesNotThrow(() -> logic.changeDiceState(0)); + } + + private void startGameAndRollDice(Answer answer) { + Random mockedRandom = Mockito.mock(Random.class); + Mockito.when(mockedRandom.nextInt(6)).thenAnswer(answer); + logic = new Logic(mockedRandom); + logic.startGame(); + logic.rollDice(); + } + private int randomNumber0or4() { - return randomNumber(n -> n != 0 && n != 4); + return randomNumberExcept(n -> n != 0 && n != 4); } private int randomNumberNot0or4() { - return randomNumber(n -> n == 0 || n == 4); + return randomNumberExcept(n -> n == 0 || n == 4); } - private int randomNumber(Predicate condition) { + private int randomNumberExcept(Predicate condition) { int randomNumber; do { randomNumber = random.nextInt(6); // 0 to 5