commit 9a8a7c79e8c674d8c65b107ed75d6b2a9fb3bbe5 Author: Thomas Papendieck Date: Tue Dec 7 10:22:23 2021 +0100 Tests für GameCell und CellState.Dead diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8eb46e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +target/ +main/ +.* diff --git a/README.md b/README.md new file mode 100644 index 0000000..ca13528 --- /dev/null +++ b/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. + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..502047e --- /dev/null +++ b/pom.xml @@ -0,0 +1,62 @@ + + 4.0.0 + de.edu.hsfulda.ciip.gameoflife + GameOfLife + 0.1.0-SNAPSHOT + + 11 + 11 + 5.8.1 + 1.8.1 + 4.1.0 + 3.21.0 + + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-params + ${junit.jupiter.version} + test + + + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.mockito + mockito-core + ${mockito.version} + test + + + org.mockito + mockito-junit-jupiter + ${mockito.version} + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + 2.22.2 + + + + + diff --git a/src/test/java/de/edu/hsfulda/ciil/cgol/GameCellTest.java b/src/test/java/de/edu/hsfulda/ciil/cgol/GameCellTest.java new file mode 100644 index 0000000..facce65 --- /dev/null +++ b/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 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 neigborStates, CellState expectedResult) { + Collections.shuffle(neigborStates); + CellState calculatedState = CELL_STATE_DEAD.calculateFuture(neigborStates); + assertThat(calculatedState).describedAs(testName).isEqualTo(expectedResult); + } + + private static Stream 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)); + } + +}