com.gargoylesoftware.base.testing
Class OrderedTestSuite

java.lang.Object
  extended by junit.framework.TestSuite
      extended by com.gargoylesoftware.base.testing.OrderedTestSuite
All Implemented Interfaces:
junit.framework.Test

public final class OrderedTestSuite
extends junit.framework.TestSuite

This class allows you to specify the order that test methods will execute in and yet still retain the ability to have test methods found via reflection.

Generally, when you want to specify the order that tests will be run, you write code that looks like this.

 public static Test suite() {
     final TestSuite suite = new TestSuite();
     suite.addTest( new FooTest("testOne") );
     suite.addTest( new FooTest("testTwo") );
     return suite;
 }
 
The problem with this approach is that when testThree() is written, if it isn't added to the suite method, it won't get executed.

Using OrderedTestSuite, you write code like this:

 public static Test suite() {
     return new OrderedTestSuite(FooTest.class, new String[]{"testOne", "testTwo"});
 }
 
testOne() and testTwo() will be executed in order just as in the first example. The difference is that OrderedTestSuite will use reflection to find other methods starting with test and will execute them. This means that if you write testThree but forget to add it to suite() then it will still get executed, albeit not in a defined order.

Version:
$Revision: 1.5 $
Author:
Mike Bowler

Field Summary
private  java.lang.Class classToTest_
           
private  java.lang.String[] orderedMethodNames_
           
 
Constructor Summary
OrderedTestSuite(java.lang.Class clazz, java.lang.String[] methodNames)
          Create an instance of this test suite
 
Method Summary
private  void addMethodsInOrder(java.util.Map methodMap)
           
private  void addTest(java.lang.reflect.Method method)
           
private  java.util.Map getMatchingMethods()
           
private  boolean isMatchingMethod(java.lang.reflect.Method method)
           
 
Methods inherited from class junit.framework.TestSuite
addTest, addTestSuite, countTestCases, createTest, getName, getTestConstructor, run, runTest, setName, testAt, testCount, tests, toString, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

classToTest_

private final java.lang.Class classToTest_

orderedMethodNames_

private final java.lang.String[] orderedMethodNames_
Constructor Detail

OrderedTestSuite

public OrderedTestSuite(java.lang.Class clazz,
                        java.lang.String[] methodNames)
Create an instance of this test suite

Parameters:
clazz - the class that we will use to get the individual tests. This must be a subclass of TestCase
methodNames - The names of any methods that must be tested in order
Method Detail

getMatchingMethods

private java.util.Map getMatchingMethods()

isMatchingMethod

private boolean isMatchingMethod(java.lang.reflect.Method method)

addTest

private void addTest(java.lang.reflect.Method method)

addMethodsInOrder

private void addMethodsInOrder(java.util.Map methodMap)