Thomas Papendieck
4 years ago
commit
e9c55c6d31
4 changed files with 169 additions and 0 deletions
-
50pom.xml
-
34src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCell.java
-
9src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeStatus.java
-
76src/test/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCellTest.java
@ -0,0 +1,50 @@ |
|||||
|
<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</groupId> |
||||
|
<artifactId>tdd-unit-with-dependencied</artifactId> |
||||
|
<version>0.1.0-SNAPSHOT</version> |
||||
|
<properties> |
||||
|
<maven.compiler.target>11</maven.compiler.target> |
||||
|
<maven.compiler.source>11</maven.compiler.source> |
||||
|
</properties> |
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.junit.jupiter</groupId> |
||||
|
<artifactId>junit-jupiter-engine</artifactId> |
||||
|
<version>5.5.2</version> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.junit.platform</groupId> |
||||
|
<artifactId>junit-platform-runner</artifactId> |
||||
|
<version>1.5.2</version> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.mockito</groupId> |
||||
|
<artifactId>mockito-core</artifactId> |
||||
|
<version>3.6.0</version> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.mockito</groupId> |
||||
|
<artifactId>mockito-junit-jupiter</artifactId> |
||||
|
<version>3.6.0</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> |
@ -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<GameOfLifeCell> 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; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
package de.edu.hsfulda.ccip.tdd.withdependencies; |
||||
|
|
||||
|
import java.util.Collection; |
||||
|
|
||||
|
public interface GameOfLifeStatus { |
||||
|
|
||||
|
GameOfLifeStatus calculateNextBy(Collection<GameOfLifeStatus> neighborStates); |
||||
|
|
||||
|
} |
@ -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<GameOfLifeCell> 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<List<GameOfLifeStatus>> 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)); |
||||
|
} |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue