【发布时间】:2011-02-21 21:26:33
【问题描述】:
我想这样做,但我做不到。这是我的情景和理性。我有一个用于测试用例的抽象类,它有一个名为 test() 的抽象方法。 test() 方法由子类定义;它是用特定应用程序的逻辑来实现的,例如CRMAppTestCase extends 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();
【问题讨论】:
-
附带说明一下,CRMAppTestCase 实例化的日期/时间和运行它的时间可能非常不同,因为它们可能会被排队或放入线程池中以在将来执行。
-
您重新发明 JUnit 有什么原因吗?
-
考虑到 test() 不是一个 junit 方法,它没有重新发明。这确实与硒有关,但这是题外话。