@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依赖注入。我对TestExecutionListeners的执行顺序有疑问。根据{{3}},可以通过Ordered接口或@Order注释和this documentation about the ordering of custom TestExecutionListeners指定顺序。但是,在执行此测试类时,beforeTestClass在任何注入之前执行。我甚至放入了mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
,因此不会错过任何默认TestExecutionListeners
。在beforeTestClass注释掉时,注入正常执行。
我的问题是,为什么我的beforeTestClass首先执行?我可以告诉它在注射后执行吗?
答案 0 :(得分:0)
不确定它是否是正确的修复程序,但它足以用于我的用例,即在beforeTestClass之前注入bean。只需通过写作强制它自动装配即可
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
testContext.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(this);
dummyWebAppControllers[0] = new DummyWebAppService(mavenPath, webAppPath, 8080);
}
似乎有点hacky强迫autowire,但嘿我的问题解决了