Task 1 b) 2 more test cases

This commit is contained in:
0nlineSam 2025-01-09 19:44:08 +01:00
parent 8f3ea48583
commit 5da1a41b2b

View File

@ -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<Integer> 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<Integer> condition) {
private int randomNumberExcept(Predicate<Integer> condition) {
int randomNumber;
do {
randomNumber = random.nextInt(6); // 0 to 5