diff --git a/dicegame/src/test/dicegame/logic/LogicTest.java b/dicegame/src/test/dicegame/logic/LogicTest.java index efd992a6..611ced20 100644 --- a/dicegame/src/test/dicegame/logic/LogicTest.java +++ b/dicegame/src/test/dicegame/logic/LogicTest.java @@ -4,17 +4,16 @@ import dicegame.logic.Logic; import dicegame.logic.SelectionException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.mockito.Mockito; -import org.mockito.stubbing.Answer; import java.util.Random; -import java.util.function.Predicate; import static org.junit.jupiter.api.Assertions.*; class LogicTest { Logic logic; - Random random = new Random(1234L); /** * K1 Passed index: @@ -50,7 +49,7 @@ class LogicTest { // The value for this index is not 1 or 5 @Test void changeDiceStateWithValidDiceNot1Or5() { - startGameAndRollDice(invocation -> randomNumberNot0or4()); + startGameAndRollDiceWithValue(2); Exception e = assertThrows(SelectionException.class, () -> logic.changeDiceState(0)); String expectedMessage = "Es können nur 1'en und 5'en fixiert werden!"; @@ -59,59 +58,63 @@ class LogicTest { } // K3 (a) - // Valid index: 0-5 - // Value: 0 | 5 - // Index refers to dice which has been fixed in previous round - @Test - void changeDiceStateWhenFixedInPrevRoll() { - startGameAndRollDice(invocation -> randomNumber0or4()); + @ParameterizedTest + @CsvSource({"1","5"}) + void changeDiceStateWithValidDice1Or5(int diceValue) { + startGameAndRollDiceWithValue(diceValue); + + // Fixing dice in round 1 assertDoesNotThrow(() -> logic.changeDiceState(0)); + + // Starting next round logic.rollDice(); - assertThrows(Exception.class, () -> logic.changeDiceState(0)); + + // Trying to fix dice again in round 2 + Exception e = assertThrows(SelectionException.class, () -> logic.changeDiceState(0)); + String expectedMessage = "Der Würfel wurde bereits in einer vorherigen Runde fixiert!"; + String actualMessage = e.getMessage(); + assertEquals(expectedMessage, actualMessage); } // 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()); + @ParameterizedTest + @CsvSource({"1","5"}) + void changeDiceStateWhenFixedInThisRoll(int diceValue) { + startGameAndRollDiceWithValue(diceValue); + assertFalse(logic.isDieFixed(0)); + + // Fixing dice assertDoesNotThrow(() -> logic.changeDiceState(0)); + assertTrue(logic.isDieFixed(0)); + + //Unfixing dice assertDoesNotThrow(() -> logic.changeDiceState(0)); + assertFalse(logic.isDieFixed(0)); } // K3 (c) // Valid index: 0-5 // Value: 0 | 5 // Index refers to dice which is not fixed - @Test - void changeDiceStateWhenNotFixed() { - startGameAndRollDice(invocation -> randomNumber0or4()); + @ParameterizedTest + @CsvSource({"1","5"}) + void changeDiceStateWhenNotFixed(int diceValue) { + startGameAndRollDiceWithValue(diceValue); + assertFalse(logic.isDieFixed(0)); + + // Fixing dice assertDoesNotThrow(() -> logic.changeDiceState(0)); + assertTrue(logic.isDieFixed(0)); } - private void startGameAndRollDice(Answer answer) { + private void startGameAndRollDiceWithValue(int value) { Random mockedRandom = Mockito.mock(Random.class); - Mockito.when(mockedRandom.nextInt(6)).thenAnswer(answer); + Mockito.when(mockedRandom.nextInt(6)).thenReturn(value - 1); logic = new Logic(mockedRandom); logic.startGame(); logic.rollDice(); } - - private int randomNumber0or4() { - return randomNumberExcept(n -> n != 0 && n != 4); - } - - private int randomNumberNot0or4() { - return randomNumberExcept(n -> n == 0 || n == 4); - } - - private int randomNumberExcept(Predicate condition) { - int randomNumber; - do { - randomNumber = random.nextInt(6); // 0 to 5 - } while (condition.test(randomNumber)); // Repeat until condition is satisfied - return randomNumber; - } } \ No newline at end of file