Task 1 changed tests to parameterized tests
This commit is contained in:
parent
800b5ef5d2
commit
04cd6a297a
@ -4,17 +4,16 @@ import dicegame.logic.Logic;
|
|||||||
import dicegame.logic.SelectionException;
|
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.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
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 static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class LogicTest {
|
class LogicTest {
|
||||||
Logic logic;
|
Logic logic;
|
||||||
Random random = new Random(1234L);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* K1 Passed index:
|
* K1 Passed index:
|
||||||
@ -50,7 +49,7 @@ class LogicTest {
|
|||||||
// 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() {
|
||||||
startGameAndRollDice(invocation -> randomNumberNot0or4());
|
startGameAndRollDiceWithValue(2);
|
||||||
|
|
||||||
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!";
|
||||||
@ -59,59 +58,63 @@ class LogicTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// K3 (a)
|
// K3 (a)
|
||||||
// Valid index: 0-5
|
@ParameterizedTest
|
||||||
// Value: 0 | 5
|
@CsvSource({"1","5"})
|
||||||
// Index refers to dice which has been fixed in previous round
|
void changeDiceStateWithValidDice1Or5(int diceValue) {
|
||||||
@Test
|
startGameAndRollDiceWithValue(diceValue);
|
||||||
void changeDiceStateWhenFixedInPrevRoll() {
|
|
||||||
startGameAndRollDice(invocation -> randomNumber0or4());
|
// Fixing dice in round 1
|
||||||
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
||||||
|
|
||||||
|
// Starting next round
|
||||||
logic.rollDice();
|
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)
|
// K3 (b)
|
||||||
// Valid index: 0-5
|
// Valid index: 0-5
|
||||||
// Value: 0 | 5
|
// Value: 0 | 5
|
||||||
// Index refers to dice which has been fixed in current round
|
// Index refers to dice which has been fixed in current round
|
||||||
@Test
|
@ParameterizedTest
|
||||||
void changeDiceStateWhenFixedInThisRoll() {
|
@CsvSource({"1","5"})
|
||||||
startGameAndRollDice(invocation -> randomNumber0or4());
|
void changeDiceStateWhenFixedInThisRoll(int diceValue) {
|
||||||
|
startGameAndRollDiceWithValue(diceValue);
|
||||||
|
assertFalse(logic.isDieFixed(0));
|
||||||
|
|
||||||
|
// Fixing dice
|
||||||
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
||||||
|
assertTrue(logic.isDieFixed(0));
|
||||||
|
|
||||||
|
//Unfixing dice
|
||||||
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
||||||
|
assertFalse(logic.isDieFixed(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// K3 (c)
|
// K3 (c)
|
||||||
// Valid index: 0-5
|
// Valid index: 0-5
|
||||||
// Value: 0 | 5
|
// Value: 0 | 5
|
||||||
// Index refers to dice which is not fixed
|
// Index refers to dice which is not fixed
|
||||||
@Test
|
@ParameterizedTest
|
||||||
void changeDiceStateWhenNotFixed() {
|
@CsvSource({"1","5"})
|
||||||
startGameAndRollDice(invocation -> randomNumber0or4());
|
void changeDiceStateWhenNotFixed(int diceValue) {
|
||||||
|
startGameAndRollDiceWithValue(diceValue);
|
||||||
|
assertFalse(logic.isDieFixed(0));
|
||||||
|
|
||||||
|
// Fixing dice
|
||||||
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
||||||
|
assertTrue(logic.isDieFixed(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startGameAndRollDice(Answer<Integer> answer) {
|
private void startGameAndRollDiceWithValue(int value) {
|
||||||
Random mockedRandom = Mockito.mock(Random.class);
|
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 = new Logic(mockedRandom);
|
||||||
logic.startGame();
|
logic.startGame();
|
||||||
logic.rollDice();
|
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<Integer> condition) {
|
|
||||||
int randomNumber;
|
|
||||||
do {
|
|
||||||
randomNumber = random.nextInt(6); // 0 to 5
|
|
||||||
} while (condition.test(randomNumber)); // Repeat until condition is satisfied
|
|
||||||
return randomNumber;
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user