Thursday, July 26, 2018

java - JUnit testing System.out values


Possible Duplicates:
Java, Junit - Capture the standard input / Output for use in a unit test
JUnit test for System.out.println()






I have written a piece of code similar to the one below. I want to unit test this method, and right now I am stuck at the System.out part. Lets say that the user input was 12. This would prompt the user with "Wrong number, try again.". Is it possible to test this output, or is this impossible by using JUnit?




        public static int testUserInput() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Give a number between 1 and 10");
int input = keyboard.nextInt();

while (input < 1 || input > 10) {
System.out.println("Wrong number, try again.");
input = keyboard.nextInt();
}


return input;
}


Thanks in advance.

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...