A TestRunner User Interface Class


At this point, the main method is no longer a couple of simple hacked-out lines. It's time to move this to a separate class responsible for presenting the user interface.

Since TestRunner is a utility for test purposes, as I mentioned earlier, tests aren't absolutely required. For the small bit of nonproduction user interface code you'll write for the test runner, you shouldn't feel compelled to test first. You're more than welcome to do so, but I'm not going to here.

The following listing shows a refactored user interface class that prints out ignored methods. About the only thing interesting in it is the "clever" way I return the number of failed tests in the System.exit call. Why? Why not? It's more succinct than an if statement, it doesn't obfuscate the code, and it returns additional information that the build script or operating system could use.

 package sis.testing; import java.lang.reflect.*; import java.util.*; public class TestRunnerUI {    private TestRunner runner;    public static void main(String[] args) throws Exception {       TestRunnerUI ui = new TestRunnerUI(args[0]);       ui.run();       System.exit(ui.getNumberOfFailedTests());    }    public TestRunnerUI(String testClassName) throws Exception {       runner = new TestRunner(testClassName);    }    public void run() {       runner.run();       showResults();       showIgnoredMethods();    }    public int getNumberOfFailedTests() {       return runner.failed();    }    private void showResults() {       System.out.println(          "passed: " + runner.passed() +          " failed: " + runner.failed());    }    private void showIgnoredMethods() {       if (runner.getIgnoredMethods().isEmpty())          return;       System.out.println("\nIgnored Methods");       for (Map.Entry<Method, Ignore> entry:             runner.getIgnoredMethods().entrySet()) {          Ignore ignore = entry.getValue();          System.out.println(entry.getKey() + ": " + ignore.value());       }    } } 



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net