commit e9c55c6d31334b528a588d66e7eac95d01f0c329 Author: Thomas Papendieck Date: Wed Nov 25 10:13:51 2020 +0100 initialer commit diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ba0acc6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + de.edu.hsfulda.ciip + tdd-unit-with-dependencied + 0.1.0-SNAPSHOT + + 11 + 11 + + + + org.junit.jupiter + junit-jupiter-engine + 5.5.2 + test + + + org.junit.platform + junit-platform-runner + 1.5.2 + test + + + org.mockito + mockito-core + 3.6.0 + test + + + org.mockito + mockito-junit-jupiter + 3.6.0 + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + 2.22.2 + + + + + diff --git a/src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCell.java b/src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCell.java new file mode 100644 index 0000000..696a47b --- /dev/null +++ b/src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCell.java @@ -0,0 +1,34 @@ +package de.edu.hsfulda.ccip.tdd.withdependencies; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.stream.Collectors; + +public class GameOfLifeCell { + + private GameOfLifeStatus status; + private final Collection neighborStates = new LinkedList<>(); + private GameOfLifeStatus nexState; + + public GameOfLifeCell(GameOfLifeStatus status) { + this.status = status; + } + + public void addNeighbor(GameOfLifeCell neighbor) { + neighborStates.add(neighbor); + } + + public void caclulateNextState() { + nexState = status + .calculateNextBy(neighborStates.stream().map(GameOfLifeCell::getState).collect(Collectors.toList())); + } + + public GameOfLifeStatus getState() { + return status; + } + + public void updateState() { + status = nexState; + } + +} diff --git a/src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeStatus.java b/src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeStatus.java new file mode 100644 index 0000000..a4eda43 --- /dev/null +++ b/src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeStatus.java @@ -0,0 +1,9 @@ +package de.edu.hsfulda.ccip.tdd.withdependencies; + +import java.util.Collection; + +public interface GameOfLifeStatus { + + GameOfLifeStatus calculateNextBy(Collection neighborStates); + +} diff --git a/src/test/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCellTest.java b/src/test/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCellTest.java new file mode 100644 index 0000000..118e039 --- /dev/null +++ b/src/test/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCellTest.java @@ -0,0 +1,76 @@ +package de.edu.hsfulda.ccip.tdd.withdependencies; + +import static org.junit.Assert.assertThat; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.hamcrest.CoreMatchers; +import org.junit.Ignore; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +class GameOfLifeCellTest { + + private GameOfLifeStatus status = Mockito.mock(GameOfLifeStatus.class, "initial state"); + + private GameOfLifeCell gameOfLifeCell; + + private List neighbors; + private GameOfLifeCell neigborCell = Mockito.mock(GameOfLifeCell.class); + + @BeforeEach + void setUp() throws Exception { + gameOfLifeCell = new GameOfLifeCell(status); + neighbors = Arrays.asList(neigborCell, neigborCell, neigborCell, neigborCell, neigborCell, neigborCell, + neigborCell, neigborCell); + + for (GameOfLifeCell neighbor : neighbors) { + gameOfLifeCell.addNeighbor(neighbor); + } + } + + @Test + void passesNeighborStatusToOwn() { + + gameOfLifeCell.caclulateNextState(); + + ArgumentCaptor> listOfNeghborStates = ArgumentCaptor.forClass(List.class); + Mockito.verify(status).calculateNextBy(listOfNeghborStates.capture()); + assertThat("all neighbor states", listOfNeghborStates.getValue().size(), + CoreMatchers.equalTo(neighbors.size())); + } + + @Test + void doesNotChangeStateWhenCalculating() { + GameOfLifeStatus newState = Mockito.mock(GameOfLifeStatus.class); + Mockito.doReturn(newState).when(status).calculateNextBy(Mockito.anyList()); + + gameOfLifeCell.caclulateNextState(); + GameOfLifeStatus currentState = gameOfLifeCell.getState(); + + Mockito.verify(status).calculateNextBy(Mockito.anyList()); + + assertThat("status not changed", currentState, CoreMatchers.equalTo(status)); + + } + + @Test + void changesStateWhenRequested() { + GameOfLifeStatus newState = Mockito.mock(GameOfLifeStatus.class, "new state"); + Mockito.doReturn(newState).when(status).calculateNextBy(Mockito.anyList()); + + gameOfLifeCell.caclulateNextState(); + gameOfLifeCell.updateState(); + + GameOfLifeStatus currentState = gameOfLifeCell.getState(); + + Mockito.verify(status).calculateNextBy(Mockito.anyList()); + + assertThat("status changed", currentState, CoreMatchers.equalTo(newState)); + } + +}