Classes:from parent to child
1.
junit
packagejunit.framework;<br><br>publicinterfaceTest { publicabstractint countTestCases(); publicabstractvoid run(TestResult result); }
Assert
package junit.framework;
public class Assert {
...
assertTrue()
assertFalse()
fail()
assertEquals()
assertNotNull()
assertNull()
...
}
fail will throw error, other's function will call fail in the end.
2. TestCase
package junit.framework;
public abstract class TestCase extends Assert implements Test {
protected void runTest() throws Throwable {
assertNotNull(fName);
Method runMethod= null;
try {
// use getMethod to get all public inherited
// methods. getDeclaredMethods returns all
// methods of this class but excludes the
// inherited ones.
runMethod= getClass().getMethod(fName, (Class[]) null);
} catch (NoSuchMethodException e) {
fail("Method \""+fName+"\" not found");
}
try {
runMethod.invoke(this, (Object[]) null);
}
....
}
public String getName() {
return fName;
}
public void setName(String name) {
fName= name;
}
protected void setUp() throws Exception {
}
protected void tearDown() throws Exception {
}
....
TestCase has a method run(), which use java reflect to get the "Class" and "Method"
then call method.invoke() to start method.
3.InstrumentationTestCase
package android.test;
public class InstrumentationTestCase extends TestCase {
....
public final <T extends Activity> T launchActivity(
String pkg,
Class<T> activityCls,
Bundle extras) {
Intent intent = new Intent(Intent.ACTION_MAIN);
if (extras != null) {
intent.putExtras(extras);
}
return launchActivityWithIntent(pkg, activityCls, intent);
}
...
public void runTestOnUiThread(final Runnable r) throws Throwable {
....
}
...
protected void runTest() throws Throwable {
...
}
private void runMethod(Method runMethod, int tolerance) throws Throwable {
runMethod(runMethod, tolerance, false);
}