How can I JUnit test my simple main method command line class which reads from system.in using Scanner and prints to system.out.
import java.util.Scanner;
public class SimpleMainCmdApplication {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.next());
}
}
This is quite a specific use case where I am doing a course data structures and algorithms course which requires participants to upload a *.java file with the main method which reads from System.in (using Scanner) and outputs answers to System.out. When you upload your code the website builds and runs the application and tests and measures the performance of your algorithm.
I wanted a way to simulate this so I can write simple integration tests using JUnit as part of taking a TDD approach to each assignment. There were some useful pointers on other stack overflow threads which helped, but nothing which met the full requirements.