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.

77 lines
2.0 KiB

  1. package de.edu.hsfulda.ciip.tdd;
  2. import static org.mockito.Mockito.any;
  3. import static org.hamcrest.CoreMatchers.equalTo;
  4. import static org.junit.Assert.*;
  5. import static org.mockito.Mockito.doReturn;
  6. import static org.mockito.Mockito.mock;
  7. import static org.mockito.Mockito.verify;
  8. import static org.mockito.Mockito.when;
  9. import java.util.Arrays;
  10. import java.util.Collection;
  11. import java.util.List;
  12. import org.hamcrest.CoreMatchers;
  13. import org.junit.Rule;
  14. import org.junit.Test;
  15. import org.mockito.ArgumentCaptor;
  16. import org.mockito.Mock;
  17. import org.mockito.junit.MockitoJUnit;
  18. import org.mockito.junit.MockitoRule;
  19. public class GameCellTest {
  20. @Rule
  21. public MockitoRule mockitoRule = MockitoJUnit.rule();
  22. @Mock
  23. State initialState;
  24. @Mock
  25. State changedState;
  26. @Test
  27. public void reportsInitiallyProvidesState() {
  28. GameCell cell = new GameCell(initialState);
  29. State result = cell.getState();
  30. assertThat("initial state returned", result, equalTo(initialState));
  31. }
  32. @Test
  33. public void changesStateWhenRequested() {
  34. GameCell cell = new GameCell(initialState);
  35. when(initialState.nextBy(any(List.class))).thenReturn(changedState);
  36. cell.calculateNextGen();
  37. verify(initialState).nextBy(any(List.class));
  38. cell.activateNextGeneration();
  39. State result = cell.getState();
  40. assertThat("initial state returned", result, equalTo(changedState));
  41. }
  42. @SuppressWarnings("unchecked")
  43. @Test
  44. public void passesNeigbourStatesForStateChangeCalculation() {
  45. GameCell cell = new GameCell(initialState);
  46. List<State> neighborStates = Arrays.asList(mock(State.class), mock(State.class), mock(State.class),
  47. mock(State.class));
  48. for (State state : neighborStates) {
  49. cell.addNeigbour(new GameCell(state));
  50. }
  51. cell.calculateNextGen();
  52. ArgumentCaptor<List> passedStates = ArgumentCaptor.forClass(List.class);
  53. verify(initialState).nextBy(passedStates.capture());
  54. List<State> statesPassed = passedStates.getValue();
  55. for (State state : neighborStates) {
  56. assertThat(statesPassed, CoreMatchers.hasItem(state));
  57. }
  58. }
  59. }