Browse Source

Loesung der Aufgabe

uebung-neues-interface
Thomas Papendieck 4 years ago
parent
commit
f88f65850e
  1. 8
      src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCell.java
  2. 7
      src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeStateChangeListener.java
  3. 33
      src/test/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCellTest.java

8
src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCell.java

@ -9,9 +9,13 @@ public class GameOfLifeCell {
private GameOfLifeStatus status;
private final Collection<GameOfLifeCell> neighborStates = new LinkedList<>();
private GameOfLifeStatus nexState;
private GameOfLifeStateChangeListener stateChangeListener;
public GameOfLifeCell(GameOfLifeStatus status) {
public GameOfLifeCell(//
GameOfLifeStatus status, //
GameOfLifeStateChangeListener stateChangeListener) {
this.status = status;
this.stateChangeListener = stateChangeListener;
}
public void addNeighbor(GameOfLifeCell neighbor) {
@ -21,6 +25,8 @@ public class GameOfLifeCell {
public void caclulateNextState() {
nexState = status
.calculateNextBy(neighborStates.stream().map(GameOfLifeCell::getState).collect(Collectors.toList()));
if (!nexState.equals(status))
stateChangeListener.stateChangedIn(this);
}
public GameOfLifeStatus getState() {

7
src/main/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeStateChangeListener.java

@ -0,0 +1,7 @@
package de.edu.hsfulda.ccip.tdd.withdependencies;
public interface GameOfLifeStateChangeListener {
void stateChangedIn(GameOfLifeCell gameOfLifeCell);
}

33
src/test/java/de/edu/hsfulda/ccip/tdd/withdependencies/GameOfLifeCellTest.java

@ -1,6 +1,7 @@
package de.edu.hsfulda.ccip.tdd.withdependencies;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import java.util.Collections;
@ -15,16 +16,20 @@ import org.mockito.Mockito;
class GameOfLifeCellTest {
private GameOfLifeStateChangeListener stateChangeListener =
Mockito.mock(GameOfLifeStateChangeListener.class);
private GameOfLifeStatus status = Mockito.mock(GameOfLifeStatus.class, "initial state");
private GameOfLifeCell gameOfLifeCell;
private List<GameOfLifeCell> neighbors;
private GameOfLifeCell neigborCell = Mockito.mock(GameOfLifeCell.class);
private GameOfLifeCell neigborCell = Mockito.mock(GameOfLifeCell.class,"neighbor");
@BeforeEach
void setUp() throws Exception {
gameOfLifeCell = new GameOfLifeCell(status);
Mockito.doReturn(status).when(status).calculateNextBy(Mockito.anyList());
gameOfLifeCell = new GameOfLifeCell(status,stateChangeListener);
neighbors = Arrays.asList(neigborCell, neigborCell, neigborCell, neigborCell, neigborCell, neigborCell,
neigborCell, neigborCell);
@ -73,4 +78,28 @@ class GameOfLifeCellTest {
assertThat("status changed", currentState, CoreMatchers.equalTo(newState));
}
@Test
void callsChangeListenerWhenStateChanges() throws Exception {
GameOfLifeStatus newState = Mockito.mock(GameOfLifeStatus.class, "new state");
Mockito
.doReturn(newState)
.when(status)
.calculateNextBy(Mockito.anyList());
gameOfLifeCell.caclulateNextState();
Mockito.verify(stateChangeListener).stateChangedIn(gameOfLifeCell);
}
@Test
void doesNotCallsChangeListenerWhenStateNotChanges() throws Exception {
Mockito.doReturn(status)
.when(status).calculateNextBy(Mockito.anyList());
gameOfLifeCell.caclulateNextState();
Mockito.verify(stateChangeListener,Mockito.never()).stateChangedIn(gameOfLifeCell);
}
}
Loading…
Cancel
Save