I don't know how to simulate user input for integers/floats/doubles inside a unit test. I used this to simulate a string input:
@Test
public void testSetName() {
String expectedResult = "Jason";
InputStream in = new ByteArrayInputStream(expectedResult.getBytes());
System.setIn(in);
assertEquals(expectedResult, MainClass.setName());
}
But I don't know how to enter an integer instead. This is the method I am testing:
public static int setAge() {
Scanner input = new Scanner(System.in);
int age = 0;
boolean done = false;
while (!done) {
System.out.print("Age: ");
try {
age = input.nextInt();
done = true;
} catch (InputMismatchException e) {
System.out.println("Please enter a valid age!");
input.nextLine();
}
}
return age;
}