HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Printing JUnit test results in file by changing and using the 'out' static variable of 'System' class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
filetheclasssystemjunitprintingtestusingandresults

Problem

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?

Solution

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.

Code Snippets

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);
}

Context

StackExchange Code Review Q#131594, answer score: 5

Revisions (0)

No revisions yet.