@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class, loader = AnnotationConfigContextLoader.class)
@TestExecutionListeners(listeners = LoadBalancingIntegrationTest.class, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public class LoadBalancingIntegrationTest extends AbstractTestExecutionListener{
//...
DummyWebAppService[] dummyWebAppControllers = new DummyWebAppService[4];
int haproxyListeningPort = 8000;
//DummyWebApp
@Value("${dummyWebApp.mvnPath}")
String mavenPath;
@Value("${dummyWebApp.webAppPath}")
String webAppPath;
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
dummyWebAppControllers[0] = new DummyWebAppService(mavenPath, webAppPath, 8080);
}
//..test cases follow
}
我在我的测试用例中使用 spring 依赖注入(inject)。我对 TestExecutionListeners 的执行顺序有疑问。根据this documentation about the ordering of custom TestExecutionListeners ,可以通过 Ordered 接口(interface)或 @Order 注解指定顺序,并且 by default, the order is of lowest predence for any custom TestExecutionListener 。然而,在执行这个测试类时,beforeTestClass会在任何注入(inject)之前执行。我什至输入了 mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
,这样就不会错过默认的 TestExecutionListeners
。当注释掉beforeTestClass时,注入(inject)正常执行。
我的问题是,为什么我的 beforeTestClass 首先被执行?我可以让它在注入(inject)后执行吗?
最佳答案
不确定这是否是正确的修复,但对于我的用例来说已经足够了,即在 beforeTestClass 之前注入(inject) beans。只需通过写入强制它 Autowiring
@Override
公共(public)无效beforeTestClass(TestContext testContext)抛出异常{
testContext.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(this);
dummyWebAppControllers[0] = new DummyWebAppService(mavenPath, webAppPath, 8080);
}
强制 Autowiring 似乎有点老套,但是嘿我的问题已经解决了
关于java - TestExecutionListener 的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44725687/