Browse Source

Tests für GameCell und CellState.Dead

master
Thomas Papendieck 3 years ago
commit
9a8a7c79e8
  1. 3
      .gitignore
  2. 5
      README.md
  3. 62
      pom.xml
  4. 84
      src/test/java/de/edu/hsfulda/ciil/cgol/GameCellTest.java
  5. 67
      src/test/java/de/edu/hsfulda/ciil/cgol/rulset/r23_3/CellStateDeadTest.java

3
.gitignore

@ -0,0 +1,3 @@
target/
main/
.*

5
README.md

@ -0,0 +1,5 @@
# Übung Test Driven Development
Dieser Code implementiert *"Conways Game of Life"* und dient als Beispiel für die Arbeitsmethode TDD.
Daher ist hier nur der Test-Code eingecheckt.

62
pom.xml

@ -0,0 +1,62 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.edu.hsfulda.ciip.gameoflife</groupId>
<artifactId>GameOfLife</artifactId>
<version>0.1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<junit.jupiter.version>5.8.1</junit.jupiter.version>
<junit.platform.version>1.8.1</junit.platform.version>
<mockito.version>4.1.0</mockito.version>
<assertj.version>3.21.0</assertj.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<!-- new assertThat() -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<version>2.22.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

84
src/test/java/de/edu/hsfulda/ciil/cgol/GameCellTest.java

@ -0,0 +1,84 @@
package de.edu.hsfulda.ciil.cgol;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Collection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class GameCellTest {
@Mock(name = "initial state")
private CellState initialState;
@Mock(name = "neigbor state")
private CellState neighborState;
@Mock(name = "new state")
private CellState calculatedState;
@Mock(name = "neighbor")
private GameCell neighborCell;
private GameCell gameCell;
private final Collection<CellState> neighborStates = Arrays
.asList(neighborState, neighborState, neighborState, neighborState);
@BeforeEach
void setUp() throws Exception {
lenient().when(neighborCell.getState())
.thenReturn(neighborState);
gameCell = new GameCell(initialState);
for (int i =0;i<neighborStates.size();i++) {
gameCell.addNeigbor(neighborCell);
}
}
@Test
void returnsInitialState() {
CellState currentState = gameCell.getState();
assertThat(currentState).describedAs("initial state").isEqualTo(initialState);
}
@Test
void passesStatesOfNeighborsWhenCalculatingFuture() {
gameCell.calculateFuture();
verify(initialState).calculateFuture(neighborStates);
CellState currentState = gameCell.getState();
assertThat(currentState)
.describedAs("state after calculation").isEqualTo(initialState);
}
@Test
void changesStateWhenRequested() {
when(initialState.calculateFuture(anyCollection()))
.thenReturn(calculatedState);
gameCell.calculateFuture();
gameCell.updateState();
CellState currentState = gameCell.getState();
assertThat(currentState)
.describedAs("state after changeRequest")
.isEqualTo(calculatedState);
}
}

67
src/test/java/de/edu/hsfulda/ciil/cgol/rulset/r23_3/CellStateDeadTest.java

@ -0,0 +1,67 @@
package de.edu.hsfulda.ciil.cgol.rulset.r23_3;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import de.edu.hsfulda.ciil.cgol.CellState;
class CellStateDeadTest {
private static final int NEIGHBOR_COUNT = 5+new Random().nextInt(10);
private static final CellState CELL_STATE_DEAD = new CellStateDead();
private static final CellState CELL_STATE_ALIVE = CellStateAlive.IS_ALIVE;
@BeforeEach
void setUp() throws Exception {
}
@ParameterizedTest(name = "[{index}] {0} -> {2}")
@MethodSource("testCasesForStateDead")
void test(String testName, List<CellState> neigborStates, CellState expectedResult) {
Collections.shuffle(neigborStates);
CellState calculatedState = CELL_STATE_DEAD.calculateFuture(neigborStates);
assertThat(calculatedState).describedAs(testName).isEqualTo(expectedResult);
}
private static Stream<Arguments> testCasesForStateDead() {
return Stream.of(
Arguments.of(
"with zero alive neighbors",
IntStream.range(0, NEIGHBOR_COUNT)
.mapToObj(i -> CELL_STATE_DEAD)
.collect(Collectors.toList()),
CELL_STATE_DEAD),
Arguments.of(
"with 2 alive neighbors",
IntStream.range(0, NEIGHBOR_COUNT)
.mapToObj(i -> i < 2 ? CELL_STATE_ALIVE : CELL_STATE_DEAD)
.collect(Collectors.toList()),
CELL_STATE_DEAD),
Arguments.of(
"with 4 alive neighbors",
IntStream.range(0, NEIGHBOR_COUNT)
.mapToObj(i -> i < 4 ? CELL_STATE_ALIVE : CELL_STATE_DEAD)
.collect(Collectors.toList()),
CELL_STATE_DEAD),
Arguments.of(
"with 3 alive neighbors",
IntStream.range(0, NEIGHBOR_COUNT)
.mapToObj(i -> i < 3 ? CELL_STATE_ALIVE : CELL_STATE_DEAD)
.collect(Collectors.toList()),
CELL_STATE_ALIVE));
}
}
Loading…
Cancel
Save