Using an empty constructor is the easiest solution. You can still override the constructor in the extended class.
But this is not optimal with all inheritance. This is why JUnit 4 uses annotations instead.
Another option is to create a helper method in the factory / util class and let this method do its job.
If you use Spring, you should consider using the @TestExecutionListeners annotation. Something like this test:
@RunWith(SpringJUnit4ClassRunner.class) @TestExecutionListeners({CustomTestExecutionListener.class, DependencyInjectionTestExecutionListener.class}) @ContextConfiguration("test-config.xml") public class DemoTest {
Spring AbstractTestExecutionListener contains, for example, this empty method, which you can override:
public void beforeTestClass(TestContext testContext) throws Exception { }
NOTE: DO NOT skip or skip DependencyInjectionTestExecutionListener when adding custom TestExecutionListeners . If you do this, all autowires will be null .
Espen May 13 '10 at 10:29 2010-05-13 10:29
source share