I'm building a framework for comparing files (in my own way) using JUnit. All test cases have been packaged in a JAR which is ran independently using a .bat
file I wrote. I needed to output the test results in a file instead of the console.
I was just using normal System.out.println()
in the TestRunner
class and also in the various test case classes for printing the output on the console.
I found a solution that I could use in the project (Method number 3 of this article). I redirected the output stream to my output file. Following is the relevant code from the TestRunner
class:
public class TestRunner {
public static void main(String[] args) {
System.out.println("Testing Started...");
// Save the System.out instance
PrintStream oldPrintStream = System.out;
FileOutputStream outFile = null;
try {
outFile = new FileOutputStream("result.txt");
PrintStream newPrintStream = new PrintStream(outFile);
System.setOut(newPrintStream);
Result result = JUnitCore.runClasses(TestSuite.class);
// Print the results in desired format
} catch (FileNotFoundException ex) {
Logger.getLogger(TestRunner.class.getName()).log(Level.SEVERE, null, ex);
} finally {
// Reset the old System.out instance
System.setOut(oldPrintStream);
System.out.println("Testing Completed! Check output folder for result.");
try {
outFile.close();
} catch (IOException ex) {
Logger.getLogger(TestRunner.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
The code is working fine but is it the correct way to do this?
*.bat
script to the desired results file? \$\endgroup\$