I have to test a method in a class which takes an input using Scanner class.
package com.math.calculator;
import java.util.Scanner;
public class InputOutpu
You can write a clear test for the command line interface by using the TextFromStandardInputStream
rule of the System Rules library.
public void MyTest {
@Rule
public final TextFromStandardInputStream systemInMock
= emptyStandardInputStream();
@Test
public void shouldTakeUserInput() {
systemInMock.provideLines("add 5", "another line");
InputOutput inputOutput = new InputOutput();
assertEquals("add 5", inputOutput.getInput());
}
}