Junit Test for InputStreamReader with Mockito

Sunny :

Can you please help me in writing the Junit test case for the below code?

public class ConsoleReader implements InputReader {
    public Cell readInput() {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter the co-ordinate Seperated by Comma");
            String coOrdinates = reader.readLine();
            String[] values=coOrdinates.split("\\,");
            return new Cell(Integer.parseInt(values[0]),Integer.parseInt(values[1]));
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return null;
    }
}
joemokenela :

You can use Mockito to mock the BufferedReader, like the example below.

BufferedReader bufferedReader = Mockito.mock(BufferedReader.class);
Mockito.when(bufferedReader.readLine()).thenReturn("1", "2", "3");
// You can mock the result based on the type of result you are expecting.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=102379&siteId=1