我想这样做,但我不能.这是我的场景和理性.我有一个抽象类的测试用例,其抽象方法叫做test(). test()方法由子类定义;它将通过某种应用程序的逻辑来实现,例如CRMAppTestCase扩展了CompanyTestCase.我不想直接调用test()方法,我希望超类调用test()方法,而子类可以调用一个调用这个方法的方法(并且还做其他工作,比如设置当前的例如,在执行测试之前的日期时间).示例代码:
public abstract class CompanyTestCase {
//I wish this would compile, but it cannot be declared private
private abstract void test();
public TestCaseResult performTest() {
//do some work which must be done and should be invoked whenever
//this method is called (it would be improper to expect the caller
// to perform initialization)
TestCaseResult result = new TestCaseResult();
result.setBeginTime(new Date());
long time = System.currentTimeMillis();
test(); //invoke test logic
result.setDuration(System.currentTimeMillis() - time);
return result;
}
}
然后延长这个….
public class CRMAppTestCase extends CompanyTestCase {
public void test() {
//test logic here
}
}
然后叫它….
TestCaseResult result = new CRMAppTestCase().performTest();