문제

I'm writing a unit test for the following method of my tic-tac-toe program. I would like to follow a TDD approach but the method requires user input.

public int[] playerMove(){
        Scanner reader = new Scanner(System.in);
        int[] move = new int[2];
        move[0] = reader.nextInt()-1;
        move[1] = reader.nextInt()-1;
        return move;
    }

My problem is that I can't input test numbers for move[0] and move[1] since it requires user input which is supplied via System.in. How would simulate this during my test?

도움이 되었습니까?

해결책

First of all, if you already have the code written, and only now you're writing the test, it's not TDD.

Regarding your problem, one way to solve this is to have the InputStream passed as a parameter to the constructor to the class you're testing, rather than hard-coding System.in.

This will enable you to create an instance of the tested class with a mock InputStream that generates whichever input you want.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top