FileDocCategorySizeDatePackage
TestSuite.javaAPI DocAndroid 1.5 API6880Wed May 06 22:41:02 BST 2009junit.framework

TestSuite

public class TestSuite extends Object implements 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.
see
Test

Fields Summary
private Vector
fTests
private String
fName
Constructors Summary
public TestSuite()
Constructs an empty TestSuite.


        	 
	  
	
public TestSuite(Class theClass, String name)
Constructs a TestSuite from the given class with the given name.

see
TestSuite#TestSuite(Class)

		this(theClass);
		setName(name);
	
public TestSuite(Class theClass)
Constructs a TestSuite from the given class. Adds all the methods starting with "test" as test cases to the suite. Parts of this method was written at 2337 meters in the Huffihutte, Kanton Uri

		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;
		Vector<String> names= new Vector<String>();
		while (Test.class.isAssignableFrom(superClass)) {
			Method[] methods= superClass.getDeclaredMethods();
			for (int i= 0; i < methods.length; i++) {
				addTestMethod(methods[i], names, theClass);
			}
			superClass= superClass.getSuperclass();
		}
		if (fTests.size() == 0)
			addTest(warning("No tests found in "+theClass.getName()));
	
public TestSuite(String name)
Constructs an empty TestSuite.

		setName(name);
	
Methods Summary
public voidaddTest(junit.framework.Test test)
Adds a test to the suite.

		fTests.addElement(test);
	
private voidaddTestMethod(java.lang.reflect.Method m, java.util.Vector names, java.lang.Class theClass)

		String name= m.getName();
		if (names.contains(name))
			return;
		if (! isPublicTestMethod(m)) {
			if (isTestMethod(m))
				addTest(warning("Test method isn't public: "+m.getName()));
			return;
		}
		names.addElement(name);
		addTest(createTest(theClass, name));
	
public voidaddTestSuite(java.lang.Class testClass)
Adds the tests from the given class to the suite

		addTest(new TestSuite(testClass));
	
public intcountTestCases()
Counts the number of test cases that will be run by this test.

		int count= 0;
		for (Enumeration e= tests(); e.hasMoreElements(); ) {
			Test test= (Test)e.nextElement();
			count= count + test.countTestCases();
		}
		return count;
	
public static junit.framework.TestcreateTest(java.lang.Class theClass, java.lang.String name)
...as the moon sets over the early morning Merlin, Oregon mountains, our intrepid adventurers type...

		Constructor 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;
	
private static java.lang.StringexceptionToString(java.lang.Throwable t)
Converts the stack trace into a string

		StringWriter stringWriter= new StringWriter();
		PrintWriter writer= new PrintWriter(stringWriter);
		t.printStackTrace(writer);
		return stringWriter.toString();

	
public java.lang.StringgetName()
Returns the name of the suite. Not all test suites have a name and this method can return null.

		return fName;
	
public static java.lang.reflect.ConstructorgetTestConstructor(java.lang.Class theClass)
Gets a constructor which takes a single String as its argument or a no arg constructor.

		Class[] args= { String.class };
		try {
			return theClass.getConstructor(args);	
		} catch (NoSuchMethodException e) {
			// fall through
		}
		return theClass.getConstructor(new Class[0]);
	
private booleanisPublicTestMethod(java.lang.reflect.Method m)

		return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
	 
private booleanisTestMethod(java.lang.reflect.Method m)

		String name= m.getName();
		Class[] parameters= m.getParameterTypes();
		Class returnType= m.getReturnType();
		return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE);
	 
public voidrun(junit.framework.TestResult result)
Runs the tests and collects their result in a TestResult.

		for (Enumeration e= tests(); e.hasMoreElements(); ) {
	  		if (result.shouldStop() )
	  			break;
			Test test= (Test)e.nextElement();
			runTest(test, result);
		}
	
public voidrunTest(junit.framework.Test test, junit.framework.TestResult result)

		test.run(result);
	
public voidsetName(java.lang.String name)
Sets the name of the suite.

param
name The name to set

		fName= name;
	
public junit.framework.TesttestAt(int index)
Returns the test at the given index

		return (Test)fTests.elementAt(index);
	
public inttestCount()
Returns the number of tests in this suite

		return fTests.size();
	
public java.util.Enumerationtests()
Returns the tests as an enumeration

		return fTests.elements();
	
public java.lang.StringtoString()

		if (getName() != null)
			return getName();
		return super.toString();
	 
private static junit.framework.Testwarning(java.lang.String message)
Returns a test which will fail and log a warning message.

		return new TestCase("warning") {
			protected void runTest() {
				fail(message);
			}
		};