Building the Suite


The next step is to construct a TestSuite object to be passed to the Swing test runner. The code to do this is simple: iterate through the results of gatherTestClassNames, create the corresponding Class object for each test class name, and add it to the suite. The test seems to be straightforward: Ensure that the suite contains the expected test classes.

 public void testCreateSuite() {    SuiteBuilder builder = new SuiteBuilder() {       public List<String> gatherTestClassNames() {          List<String> classNames = new ArrayList<String>();          classNames.add("testing.SuiteBuilderTest");          return classNames;       }    };    TestSuite suite = builder.suite();    assertEquals(1, suite.testCount());    assertTrue(contains(suite, testing.SuiteBuilderTest.class)); } 

I used a mock instead of letting the suite method gather all classes normally. Otherwise, gatherTestClassNames would return the entire list of classes on the classpath. This list would include all other tests in the student information system. You would have no definitive way to prove that the suite method was doing its job.

Determining whether or not a class is included in a TestSuite is not as trivial as you might expect. A test suite can either contain test case classes or other test suites. Figure 12.2 shows this design, known as a composite.[7] The filled-in diamond represents a composition relationship between TestSuite and Test. A TestSuite is composed of Test objects.

[7] [Gamma1995].

Figure 12.2. JUnit Composite Design


To determine if a class is contained somewhere within a suite, you must traverse the entire hierarchy of suites. The contains method here uses recursion, since contains calls itself when encountering a suite within the current suite.

 public boolean contains(TestSuite suite, Class testClass) {    List testClasses = Collections.list(suite.tests());    for (Object object: testClasses) {       if (object.getClass() == TestSuite.class)          if (contains((TestSuite)object, testClass))             return true;       if (object.getClass() == testClass)          return true;    }    return false; } 

All that is left is for you to create a TestRunner class. TestRunner will use the SuiteBuilder to construct a suite. It will then execute the Swing test runner using this suite. There are no testsyou'll exercise the code as part of continually running JUnit. Writing tests for this code would be possible but difficult. The way you constructed SuiteBuilder, however, means that the TestRunner code is so small, it's almost breakproof.

 package sis.testing; public class TestRunner {    public static void main(String[] args) {       new junit.swingui.TestRunner().run(TestRunner.class);    }    public static junit.framework.Test suite() {       return new SuiteBuilder().suite();    } } 

You'll want to add a target to your build script that executes testing.TestRunner:

 <target name="runAllTests" depends="build">   <java classname="testing.TestRunner" fork="yes">     <classpath ref />   </java> </target> 

When you run all the tests, however, you get about half a dozen failures.



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