0

I have TicTacToe game in java language and I have to do Junit test cases for this Game class, because I need to apply mutation testing for this application. can anyone help me to write good test cases.

public class Game {
private Board board;
private Queue<Player> players;

public Game() {
    board = new Board();
    players = new LinkedList<>();

    addPlayers();

}

private void addPlayers() {
    players.add(new Player("X"));
    players.add(new Player("O"));
}


private void startGame() throws IOException {
 do {
     Player currentPlayer = players.remove();
     System.out.println("Enter position for Player "+currentPlayer.toString());

     java.io.BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

     board.placeMarker(currentPlayer,new Position(Integer.parseInt(in.readLine()),Integer.parseInt(in.readLine())));
     players.add(currentPlayer);
     System.out.println(board.toString());
     if(board.hasWon(currentPlayer)){
         break;
     }
 }while (board.hasEmptyPosition());
}




public void main(String[] args) {
    Game game= new Game();
    try {
        game.startGame();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

1 Answers1

0

this is a very simple application, so there aren't a lot of possible tests to do.

first you should focus on testing the public API of a class, but in your case you only have main. Secondly, you depend on external input, which makes it harder to test your code...

I recommend you to read some articles on how to make your code more testable. For instance:

A few starters:

  1. Create a class responsible for starting the app (having the main method): let's call it class Application
  2. Make Game startGame() method receive as input a function that returns the input (in your PROD code you'll use the BufferedReader, in test code you could use a stream for instance
  3. same idea for the addPlayers method
  4. Extract a method for each play (basically the code inside the do...while) so that you can test it as well

and maybe you can find a few more tasks

ps: but in the end, with such a basic codebase, this is kinda overkill...

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82