You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.1 KiB
105 lines
3.1 KiB
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;
|
|
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 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,"neighbor");
|
|
|
|
@BeforeEach
|
|
void setUp() throws Exception {
|
|
Mockito.doReturn(status).when(status).calculateNextBy(Mockito.anyList());
|
|
gameOfLifeCell = new GameOfLifeCell(status,stateChangeListener);
|
|
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));
|
|
}
|
|
|
|
@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);
|
|
|
|
}
|
|
}
|