For example, with the following main method.
public class Solution {
public static void main(String []argv) {
System.out.println("Hello World.");
System.out.println("Hello Java.");
}
}
I want to know how code a platform (Hackerrank, for example) would check that the main method prints the expected output.
Hello World.
Hello Java.
Is not easy imagine how to do it with Junit, at first sight. I believe that an external program is checking the output, I mean something like this:
$ java Solution | runtests
Thank you
Your guess using unix pipe-lining is certainly a way that it could be done.
In Java, you could do it by running the java
command and reading the output through an InputStream
// Create and start the process
Process pb = new ProcessBuilder("java", "Solution").start();
// Create a reader to read in each output line
BufferedReader reader = new BufferedReader(new InputStreamReader(pb.getInputStream()));
String line;
// Read in line by line
while((line = reader.readLine()) != null) {
// ...
}
Note that the start()
method of ProcessBuilder
can throw an IOException
, so you will need to account for that.