I'm new to programmming and I have this simple method:
public double input() {
double result = 0;
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextDouble()) {
result = scanner.nextDouble();
} else {
System.out.print("Please, type numbers!\n");
}
return result;
}
The question is how to simulate (emulate) user's input from the keyboard in junit test.
Pass a
Scanner
as input parameter to the method you want to test. In your test code, you can create aScanner
instance from a string:And then in the production code, you can pass
new Scanner(System.in)
to the method.