I have only one method main. How to check System.out.println() and replace Scanner to input values automatically using JUnit?
P.S. Please, provide some solutions…
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[4];
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
for (int i = 0; i < arr.length; i++) {
int res = 0;
int k = 0;
int num = arr[i];
/*.....*/
System.out.println(num);
}
}
Ideally, extract the awkward dependencies so that you can test without them. Change
mainto simply:(Consider using a
Writerinstead of aPrintStreamforoutput.)Then you don’t really need to unit test
main– but you can testdoWorkusing aScannerbased on aStringReader, and output based on aStringWriter, providing whatever input you want and checking the output.