There is a class that I want to test, but when I use mock , I don’t get a normal replacement, that is, the resulting method in the getWinner() class does not return the expected value. When I use a real object, everything works. What am I not using mock correctly?
The class itself:
public class TicTacToe implements TicTacToeStart { // хранит партии которые можно будет сыграть в процессе игры private ArrayList<Play> games = new ArrayList<>(); // хранит объекты игроков которые победили private ArrayList<Subject> winners = new ArrayList<>(); //имя единственного победителя набравшего 5 побед private String winner; // for test void setGames(ArrayList games) { this.games = games; } // инициализирует объеты партий public void createGames() { for (int i = 0; i < 100; i++) { this.games.add( new Game()); } } // возвращает имя победителя @Override public String getWinner() { return this.winner; } // зацикливает игры до 5 побед @Override public void start() { int count = 0; while (checkWinner().equals("") && count < games.size()) { Play game = this.games.get(count); game.choiceSide(); game.loopMove(); Subject winner = game.initWinner(); if (!winner.getName().equals("nobody")) { this.winners.add(winner); } count++; } this.winner = checkWinner(); } // проверяет кто победил 5 раз и возвращает имя победителя private String checkWinner() { int user = 0, bot = 0; for (Subject winner : winners) { if ("user".equals(winner.getName())) { user++; if (user == 5) return "user"; } if ("bot".equals(winner.getName())) { bot++; if (bot == 5) return "bot"; } } return ""; } } Test:
@Test public void whenThen() { // подготовка StubInput input = new StubInput(); input.setAnswersStr(new String[] { "I", "y", "I", "y", "I", "y", "I", "y", "I", "y"}); input.setAnswersNum(new int[] { 1, 0, 2, 0, 1, 1, 2, 2, 1, 0, 2, 0, 1, 1, 2, 2, 1, 0, 2, 0, 1, 1, 2, 2, 1, 0, 2, 0, 1, 1, 2, 2, 1, 0, 2, 0, 1, 1, 2, 2}); Dialog dialog = new Dialog(); dialog.setInput(input); Game game = new Game(); game.setDialogs(dialog); ArrayList<Play> games = new ArrayList<>(); for (int i = 0; i < 100; i++) { games.add(game); } TicTacToe ticTac = new TicTacToe(); TicTacToe spy = spy(ticTac); spy.setGames(games); spy.start(); // действие String result = spy.getWinner(); // проверка что метод вызвался и что вернул что надо но // я хочу как то проверить это-же только без строчки // spy.setGames(games); и этого огромного стаба verify(spy).getWinner(); assertEquals(result, "bot"); }