Issue
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;
}
}
Solution
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.
Answered By - joemokenela