Skip to content

TicTacToe Game Unit Tests #12

@BelfDev

Description

@BelfDev

Hey guys,

I would like to discuss about the TicTacToeTest which I have coded and then, deleted. I would love to hear your point of view regarding my thoughts below:

With the TicTacToe Game, we find ourselves in a different context; unlike the previous exercises, we are now talking about a self contained, well defined program. Thus, we need to treat it in a different way.

My first impulse was to declare every internal method of the class public. As you can probably notice, this doesn't make any sense. I say this because no class that has instantiated our TicTacToe game object should be able to requestPlayerMove(...) or use any other internal mechanisms that make the game actually work.

All the methods contained in TicTacToe other than play() should be marked as private.

Obviously, this situation raises some issues regarding unit testing. I have found some resources where other developers claim that:

You need to test the class functionality, not its implementation. Wanna test the private methods? Test the public methods that call them. If the functionality the class offers is tested thoroughly, the internals of it have demonstrated to be correct and reliable; you don't need to test the internal conditions. The tests should maintain decoupling from the tested classes.

OK. This means we have to thoroughly test the play() method. Creating a unit test method for each state we would like to test.

This raises another problem: I need keyboard input in order to play the game. Again, I did some research and found this way out:

@Test
public void shouldTakeUserInput() {
    InputOutput inputOutput= new InputOutput();

    String input = "add 5";
    InputStream in = new ByteArrayInputStream(input.getBytes());
    System.setIn(in);

    assertEquals("add 5", inputOutput.getInput());
}

Although this practically solves our issues, I found myself asking whether I would actually be forced to simulate an entire match.

Well, all these questions were very enriching, but also showed me that unit tests for the TicTacToe would be extremely time consuming. I want to hear your thoughts and implement it in a near future (HELP WANTED).

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    ProgrammingCOMPSCI4039 | UofG MSc | Programminghelp wantedExtra attention is neededtech debtSomething that was postponed for the future

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions