Task 1 b) 2 more test cases
This commit is contained in:
parent
8f3ea48583
commit
5da1a41b2b
@ -5,6 +5,7 @@ import dicegame.logic.SelectionException;
|
|||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
@ -44,15 +45,12 @@ class LogicTest {
|
|||||||
logic = null;
|
logic = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Input a valid index: 0-5
|
// K2 (c)
|
||||||
|
// Valid index: 0-5
|
||||||
// The value for this index is not 1 or 5
|
// The value for this index is not 1 or 5
|
||||||
@Test
|
@Test
|
||||||
void changeDiceStateWithValidDiceNot1Or5() {
|
void changeDiceStateWithValidDiceNot1Or5() {
|
||||||
Random mockedRandom = Mockito.mock(Random.class);
|
startGameAndRollDice(invocation -> randomNumberNot0or4());
|
||||||
Mockito.when(mockedRandom.nextInt(6)).thenAnswer(invocation -> randomNumberNot0or4());
|
|
||||||
logic = new Logic(mockedRandom);
|
|
||||||
logic.startGame();
|
|
||||||
logic.rollDice();
|
|
||||||
|
|
||||||
Exception e = assertThrows(SelectionException.class, () -> logic.changeDiceState(0));
|
Exception e = assertThrows(SelectionException.class, () -> logic.changeDiceState(0));
|
||||||
String expectedMessage = "Es können nur 1'en und 5'en fixiert werden!";
|
String expectedMessage = "Es können nur 1'en und 5'en fixiert werden!";
|
||||||
@ -60,27 +58,56 @@ class LogicTest {
|
|||||||
assertEquals(expectedMessage, actualMessage);
|
assertEquals(expectedMessage, actualMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// K3 (a)
|
||||||
|
// Valid index: 0-5
|
||||||
|
// Value: 0 | 5
|
||||||
|
// Index refers to dice which has been fixed in previous round
|
||||||
@Test
|
@Test
|
||||||
void changeDiceStateWhenFixedInPrevRoll() {
|
void changeDiceStateWhenFixedInPrevRoll() {
|
||||||
Random mockedRandom = Mockito.mock(Random.class);
|
startGameAndRollDice(invocation -> randomNumber0or4());
|
||||||
Mockito.when(mockedRandom.nextInt(6)).thenAnswer(invocation -> randomNumber0or4());
|
|
||||||
logic = new Logic(mockedRandom);
|
|
||||||
logic.startGame();
|
|
||||||
logic.rollDice();
|
|
||||||
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
||||||
logic.rollDice();
|
logic.rollDice();
|
||||||
assertThrows(Exception.class, () -> logic.changeDiceState(0));
|
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() {
|
private int randomNumber0or4() {
|
||||||
return randomNumber(n -> n != 0 && n != 4);
|
return randomNumberExcept(n -> n != 0 && n != 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int randomNumberNot0or4() {
|
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;
|
int randomNumber;
|
||||||
do {
|
do {
|
||||||
randomNumber = random.nextInt(6); // 0 to 5
|
randomNumber = random.nextInt(6); // 0 to 5
|
||||||
|
Loading…
x
Reference in New Issue
Block a user