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.
To solve this I created a private method from which I can pass the command line inputs and the name of the main method class I want to invoke with reflection. I can then apply standard hamcrest macthers to the output.
In the actual implementation I also measure the time it takes to execute the code and am able to set threshold limits to test against.