5
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ Couldn't you pipe the results in your *.bat script to the desired results file? \$\endgroup\$ – h.j.k. Jun 10 '16 at 15:33
  • 1
    \$\begingroup\$ @h.j.k. Yes, but the generated result is going to be used further. So, this isn't the end. \$\endgroup\$ – Bilesh Ganguly Jun 13 '16 at 5:13
5
\$\begingroup\$

Looks good enough, but there's some improvements in the io API you're not using. Consider:

try (OutputStream outFile = Files.newOutputStream(Paths.get("result.txt"), 
       StandardOpenOption.WRITE, StandardOpenOption.CREATE);
     PrintStream newSysOut = new PrintStream(outFile)) {
     System.setOut(newSysOut);
     Result result = JUnitCore.runClasses(TestSuite.class);
     printResults(result);
} catch (IOException ex) {
     // your code here
} finally {
     System.setOut(oldPrintStream);
}

This should give you more fine-grained Exceptions when your operations fail (not that I'm using it here).
Also you don't need to close the Resources you use yourself, because of the try-with-resources-block.

Small additional note: Your comments are extraneous for this example. I'd suggest removing them.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.