如何使 @BeforeClass 在 Spring TestContext 加载之前运行?
对于使用 testNG 的程序员来说这应该是小菜一碟。我有这种情况
@ContextConfiguration(locations={"customer-form-portlet.xml", "classpath:META-INF2/base-spring.xml" })
public class BaseTestCase extends AbstractTestNGSpringContextTests {
...
@BeforeClass
public void setUpClass() throws Exception {
,但我需要在 @BeforeClass 之后加载 spring 上下文。我想出了重写 AbstractTestNGSpringContextTests 方法:
@BeforeClass(alwaysRun = true)
protected void springTestContextBeforeTestClass() throws Exception {
this.testContextManager.beforeTestClass();
}
@BeforeClass(alwaysRun = true, dependsOnMethods = "springTestContextBeforeTestClass")
protected void springTestContextPrepareTestInstance() throws Exception {
this.testContextManager.prepareTestInstance(this);
}
并制作我的方法
@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
protected void springTestContextPrepareTestClass() throws Exception {
}
但后来我得到:
由:org.testng.TestNGException引起: org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance() 不允许依赖受保护的 空白 org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestClass() 抛出 java.lang.Exception
将其公开也没有帮助。如果可以以工作方式完成,请有人在这里提及:-) 我知道我可以手动加载 testContext,但这不会那么花哨。
它的工作原理如下,但 TestContextManager 不可见,因此我无法对其调用prepareTestInstance() 方法:
@Override
@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
public void springTestContextPrepareTestInstance() throws Exception {
}
it should be piece of cake for programmers using testNG. I have this scenario
@ContextConfiguration(locations={"customer-form-portlet.xml", "classpath:META-INF2/base-spring.xml" })
public class BaseTestCase extends AbstractTestNGSpringContextTests {
...
@BeforeClass
public void setUpClass() throws Exception {
But I'd need the spring context to be load up after @BeforeClass. I I came up with overriding AbstractTestNGSpringContextTests methods :
@BeforeClass(alwaysRun = true)
protected void springTestContextBeforeTestClass() throws Exception {
this.testContextManager.beforeTestClass();
}
@BeforeClass(alwaysRun = true, dependsOnMethods = "springTestContextBeforeTestClass")
protected void springTestContextPrepareTestInstance() throws Exception {
this.testContextManager.prepareTestInstance(this);
}
and make my method
@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
protected void springTestContextPrepareTestClass() throws Exception {
}
But then I get :
Caused by: org.testng.TestNGException:
org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance()
is not allowed to depend on protected
void
org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestClass()
throws java.lang.Exception
Make it public also doesn't help. Could please anybody mention here if it can be done in a working manner :-) I know that I could load the testContext manually, but that wouldn't be so fancy.
It works like this, but TestContextManager is not visible so I can't call prepareTestInstance() method on it :
@Override
@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
public void springTestContextPrepareTestInstance() throws Exception {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我创建了自定义 DependencyInjectionTestExecutionListener,并且我已经重写了jectDependency() 方法并在那里完成了我的初始化代码
并且
Well I created custom DependencyInjectionTestExecutionListener and I have overriden injectDependencies() method and done my init code in there
AND