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.

62 lines
1.8 KiB

  1. package de.tims.tictactoe.ai;
  2. import static org.mockito.Mockito.*;
  3. import org.junit.jupiter.api.Test;
  4. import org.junit.jupiter.api.extension.ExtendWith;
  5. import org.mockito.ArgumentMatcher;
  6. import org.mockito.Mock;
  7. import org.mockito.junit.jupiter.MockitoExtension;
  8. import de.tims.tictactoe.GameLogic;
  9. @ExtendWith(MockitoExtension.class)
  10. class AIEasyTest {
  11. static int size = 3;
  12. @Mock
  13. private GameLogic gl;
  14. @Test
  15. void emptyBoardChooseRandomField() {
  16. char realChar = 'o';
  17. doReturn(new char[][] { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} }).when(gl).getBoard();
  18. TicTacToeAI ai = new AIEasy(gl);
  19. //run method 100 times, because of random generator
  20. for (int i = 0; i < 100; i++) {
  21. ai.calculateNextMove();
  22. }
  23. verify(gl, times(100)).setField(intThat(new ChooseRandomFieldMatcher()), intThat(new ChooseRandomFieldMatcher()), eq(realChar));
  24. }
  25. @Test
  26. void notEmptyBoardChooseRandomFreeField() {
  27. char realChar = 'o';
  28. doReturn(new char[][] { {'x', '-', 'o'}, {'-', 'o', '-'}, {'-', 'x', 'x'} }).when(gl).getBoard();
  29. TicTacToeAI ai = new AIEasy(gl);
  30. //run method 100 times, because of random generator
  31. for (int i = 0; i < 100; i++) {
  32. ai.calculateNextMove();
  33. }
  34. verify(gl, times(100)).setField(intThat(new ChooseRandomFieldMatcher()), intThat(new ChooseRandomFieldMatcher()), eq(realChar));
  35. //verify that the method is never called with a field which was already set
  36. verify(gl, never()).setField(0, 0, realChar);
  37. verify(gl, never()).setField(0, 2, realChar);
  38. verify(gl, never()).setField(1, 1, realChar);
  39. verify(gl, never()).setField(2, 1, realChar);
  40. verify(gl, never()).setField(2, 2, realChar);
  41. }
  42. private static class ChooseRandomFieldMatcher implements ArgumentMatcher<Integer> {
  43. @Override
  44. public boolean matches(Integer argument) {
  45. return argument.intValue() >= 0 && argument.intValue() < size;
  46. }
  47. }
  48. }