KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > runners > AllTests


1 package org.junit.runners;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.lang.reflect.Modifier JavaDoc;
6
7 import junit.framework.Test;
8 import org.junit.internal.runners.OldTestClassRunner;
9
10 /** Runner for use with JUnit 3.8.x-style AllTests classes
11  * (those that only implement a static <code>suite()</code>
12  * method). For example:
13  * <pre>
14  * &#064;RunWith(AllTests.class)
15  * public class ProductTests {
16  * public static junit.framework.Test suite() {
17  * ...
18  * }
19  * }
20  * </pre>
21  */

22 public class AllTests extends OldTestClassRunner {
23     @SuppressWarnings JavaDoc("unchecked")
24     public AllTests(Class JavaDoc<?> klass) throws Throwable JavaDoc {
25         super(testFromSuiteMethod(klass));
26     }
27
28     public static Test testFromSuiteMethod(Class JavaDoc<?> klass) throws Throwable JavaDoc {
29         Method JavaDoc suiteMethod= null;
30         Test suite= null;
31         try {
32             suiteMethod= klass.getMethod("suite");
33             if (! Modifier.isStatic(suiteMethod.getModifiers())) {
34                 throw new Exception JavaDoc(klass.getName() + ".suite() must be static");
35             }
36             suite= (Test) suiteMethod.invoke(null); // static method
37
} catch (InvocationTargetException JavaDoc e) {
38             throw e.getCause();
39         }
40         return suite;
41     }
42 }
43
Popular Tags