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?

asked Jun 10, 2016 at 6:23
\$\endgroup\$
2
  • \$\begingroup\$ Couldn't you pipe the results in your *.bat script to the desired results file? \$\endgroup\$ Commented Jun 10, 2016 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\$ Commented Jun 13, 2016 at 5:13

1 Answer 1

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.

answered Jun 10, 2016 at 9:57
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.