我有几十个测试,所有测试都使用相同的配置和监听器。这意味着重复以下行:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebDriverConfig.class)
@TestExecutionListeners(listeners = {ScreenshotTaker.class, DependencyInjectionTestExecutionListener.class})
我创建了以下内容:
public class WebDriverRunner extends SpringJUnit4ClassRunner {
public WebDriverRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
@Override
protected TestContextManager createTestContextManager(Class<?> clazz) {
return super.createTestContextManager(ConfigShim.class);
}
@ContextConfiguration(classes = WebDriverConfig.class)
@TestExecutionListeners(listeners = {ScreenshotTaker.class, DependencyInjectionTestExecutionListener.class})
public static class ConfigShim {
}
}
这意味着我可以按如下方式运行测试:
@RunWith(WebDriverRunner.class)
public class ShoppingCartPageIT {
但是,这会改变测试的名称。
答案 0 :(得分:2)
如果您使用Spring Framework 4.1或更高版本,则可以创建自己的composed annotation以进行测试配置:
定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ContextConfiguration(classes = WebDriverConfig.class)
@TestExecutionListeners({
ScreenshotTaker.class,
DependencyInjectionTestExecutionListener.class
})
public @interface WebDriverTestConfig {}
然后注释您的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@WebDriverTestConfig
public class ShoppingCartPageIT { /* ... */ }