使用空的构造函数是最简单的解决方案。您仍然可以覆盖扩展类中的构造函数。
但是对于所有的继承来说它并不是最优的。这就是为什么JUnit 4使用注释代替。
另一种选择是在factory/util类中创建一个辅助方法,并让该方法完成工作。
如果您使用的是Spring,则应考虑使用@TestExecutionListeners
注释。 事情是这样的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({CustomTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class})
@ContextConfiguration("test-config.xml")
public class DemoTest {
Spring的AbstractTestExecutionListener
例如包含您可以覆盖这个空方法:
public void beforeTestClass(TestContext testContext) throws Exception {
/* no-op */
}
出于兴趣你为什么试图做到这一点? – Goibniu 2010-05-13 09:35:46
那么,我有一个相当大的测试文件和基础测试文件的层次结构,我需要在子测试类中覆盖此操作的可能性。 – Roman 2010-05-13 09:39:29
我有同样的问题,其中只有许多参数化测试中的第一个应该执行登录。 – dokaspar 2012-10-05 14:27:57