junit.framework
public class: TestSuite [javadoc |
source]
java.lang.Object
junit.framework.TestSuite
All Implemented Interfaces:
Test
A TestSuite is a Composite of Tests.
It runs a collection of test cases. Here is an example using
the dynamic test definition.
TestSuite suite= new TestSuite();
suite.addTest(new MathTest("testAdd"));
suite.addTest(new MathTest("testDivideByZero"));
Alternatively, a TestSuite can extract the tests to be run automatically.
To do so you pass the class of your TestCase class to the
TestSuite constructor.
TestSuite suite= new TestSuite(MathTest.class);
This constructor creates a suite with all the methods
starting with "test" that take no arguments.
A final option is to do the same for a large array of test classes.
Class[] testClasses = { MathTest.class, AnotherTest.class }
TestSuite suite= new TestSuite(testClasses);
| Constructor: |
public TestSuite() {
}
Constructs an empty TestSuite. |
public TestSuite(Class<TestCase> theClass) {
fName= theClass.getName();
try {
getTestConstructor(theClass); // Avoid generating multiple error messages
} catch (NoSuchMethodException e) {
addTest(warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()"));
return;
}
if (!Modifier.isPublic(theClass.getModifiers())) {
addTest(warning("Class "+theClass.getName()+" is not public"));
return;
}
Class< ? > superClass= theClass;
List< String > names= new ArrayList< String >();
while (Test.class.isAssignableFrom(superClass)) {
for (Method each : superClass.getDeclaredMethods())
addTestMethod(each, names, theClass);
superClass= superClass.getSuperclass();
}
if (fTests.size() == 0)
addTest(warning("No tests found in "+theClass.getName()));
}
Constructs a TestSuite from the given class. Adds all the methods
starting with "test" as test cases to the suite.
Parts of this method were written at 2337 meters in the Hueffihuette,
Kanton Uri |
public TestSuite(String name) {
setName(name);
}
Constructs an empty TestSuite. |
public TestSuite(Class<?> classes) {
for (Class< ? > each : classes)
addTest(new TestSuite(each.asSubclass(TestCase.class)));
}
Constructs a TestSuite from the given array of classes. |
public TestSuite(Class<TestCase> theClass,
String name) {
this(theClass);
setName(name);
}
Constructs a TestSuite from the given class with the given name. Also see:
- TestSuite#TestSuite(Class)
|
public TestSuite(Class<TestCase>[] classes,
String name) {
this(classes);
setName(name);
}
Constructs a TestSuite from the given array of classes with the given name. Also see:
- TestSuite#TestSuite(Class[])
|
| Method from junit.framework.TestSuite Summary: |
|---|
|
addTest, addTestSuite, countTestCases, createTest, getName, getTestConstructor, run, runTest, setName, testAt, testCount, tests, toString, warning |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from junit.framework.TestSuite Detail: |
public void addTest(Test test) {
fTests.add(test);
}
Adds a test to the suite. |
public void addTestSuite(Class<TestCase> testClass) {
addTest(new TestSuite(testClass));
}
Adds the tests from the given class to the suite |
public int countTestCases() {
int count= 0;
for (Test each : fTests)
count+= each.countTestCases();
return count;
}
Counts the number of test cases that will be run by this test. |
public static Test createTest(Class<TestCase> theClass,
String name) {
Constructor< ? extends TestCase > constructor;
try {
constructor= getTestConstructor(theClass);
} catch (NoSuchMethodException e) {
return warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()");
}
Object test;
try {
if (constructor.getParameterTypes().length == 0) {
test= constructor.newInstance(new Object[0]);
if (test instanceof TestCase)
((TestCase) test).setName(name);
} else {
test= constructor.newInstance(new Object[]{name});
}
} catch (InstantiationException e) {
return(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")"));
} catch (InvocationTargetException e) {
return(warning("Exception in constructor: "+name+" ("+exceptionToString(e.getTargetException())+")"));
} catch (IllegalAccessException e) {
return(warning("Cannot access test case: "+name+" ("+exceptionToString(e)+")"));
}
return (Test) test;
}
...as the moon sets over the early morning Merlin, Oregon
mountains, our intrepid adventurers type... |
public String getName() {
return fName;
}
Returns the name of the suite. Not all
test suites have a name and this method
can return null. |
public static Constructor<TestCase> getTestConstructor(Class<TestCase> theClass) throws NoSuchMethodException {
try {
return theClass.getConstructor(String.class);
} catch (NoSuchMethodException e) {
// fall through
}
return theClass.getConstructor(new Class[0]);
}
Gets a constructor which takes a single String as
its argument or a no arg constructor. |
public void run(TestResult result) {
for (Test each : fTests) {
if (result.shouldStop() )
break;
runTest(each, result);
}
}
Runs the tests and collects their result in a TestResult. |
public void runTest(Test test,
TestResult result) {
test.run(result);
}
|
public void setName(String name) {
fName= name;
}
Sets the name of the suite. |
public Test testAt(int index) {
return fTests.get(index);
}
Returns the test at the given index |
public int testCount() {
return fTests.size();
}
Returns the number of tests in this suite |
public Enumeration<Test> tests() {
return fTests.elements();
}
Returns the tests as an enumeration |
public String toString() {
if (getName() != null)
return getName();
return super.toString();
}
|
public static Test warning(String message) {
return new TestCase("warning") {
@Override
protected void runTest() {
fail(message);
}
};
}
Returns a test which will fail and log a warning message. |