首先在TestSuite中的 public TestSuite(final Class theClass) {
fName= theClass.getName();设置断点,在这里设想,这个theClass是junit给我们传过来的,就是我们自己的那个测试类所对应的Class对象,单步执行,会到下面那段代码里面,这里会生成我们的测试类所对应的对象,采用反射的方式
static public Test createTest(Class theClass, String name) {
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;
}
当生成对象之后把它们加入到suite里面去,最后才是一个一个的去执行的,才会到TestCase里面的方法中去执行
public void run(TestResult result) {
result.run(this);
}