java 使用 maven-surefire 运行测试时,Spring-Autowiring 在 @BeforeClass 之后发生
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5192562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring-Autowiring happens after @BeforeClass when running test with maven-surefire
提问by thofoer
I have some problems with dependency injection (Spring autowiring) and maven-surefire.
The following test works without problems when run in eclipse with TestNG:
The service-object is injected, then the @BeforeClass
-method is called.
我在依赖注入(Spring 自动装配)和 maven-surefire 方面遇到了一些问题。使用 TestNG 在 Eclipse 中运行时,以下测试没有问题:注入服务对象,然后@BeforeClass
调用 -method。
@TransactionConfiguration(defaultRollback=false)
@ContextConfiguration(locations={"/testContext.xml"})
public class MyServiceTest extends AbstractTransactionalTestNGSpringContextTests {
@Autowired
private MyService service;
@BeforeTest
public void setup() {
System.out.println("*********************"+service);
Assert.assertNotNull(service);
}
However, when I run the very same testcase with maven-surefire, first setup() is called, which causes the test to fail:
但是,当我使用 maven-surefire 运行完全相同的测试用例时,会调用第一个 setup(),这会导致测试失败:
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ myserver ---
[INFO] Surefire report directory: D:\...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
**************************null
2011-03-04 11:08:57,462 DEBUG ionTestExecutionListener.prepareTestInstance - Performing dependency injection for test context [[TestContext@1fd6bea...
2011-03-04 11:08:57,462 DEBUG ractGenericContextLoader.loadContext - Loading ApplicationContext for locations [classpath:/testContext.xml].
How can I solve this problem?
If I replace @BeforeClass
with @Test
it works in maven as in TestNG's eclipse plugin.
我怎么解决这个问题?如果我@BeforeClass
用@Test
它替换它可以在 Maven 中工作,就像在 TestNG 的 eclipse 插件中一样。
maven-surefire-plugin:2.7.2
maven-surefire-plugin:2.7.2
Eclipse: Helios Service Release 1
Eclipse:Helios 服务第 1 版
jdk1.6.0_14
jdk1.6.0_14
TestNG: 5.14.10
测试版:5.14.10
回答by Aidan O
Additionally, until this issueis fixed, if things still aren't working for you after following the previous advice OR you do not wish your code to be executed before every single method, then add the following code to your test class:
此外,在此问题解决之前,如果在遵循先前的建议后仍然无法正常工作,或者您不希望在每个方法之前执行代码,请将以下代码添加到您的测试类中:
@Override
@BeforeSuite
protected void springTestContextPrepareTestInstance() throws Exception {
super.springTestContextPrepareTestInstance();
}
This ensures that the Spring Context will be prepared before your @BeforeClass methods are executed.
这可确保在执行 @BeforeClass 方法之前准备好 Spring Context。
*note, I posted this answer since in the title you're asking about @BeforeClass, even though there is no usage of @BeforeClass in your sample code.
*注意,我发布了这个答案,因为在标题中你问的是 @BeforeClass,即使在你的示例代码中没有使用 @BeforeClass。
回答by Cedric Beust
Use @BeforeMethod
, not @BeforeTest
.
使用@BeforeMethod
,不是@BeforeTest
。
回答by Sam Brannen
I agree with Cedric: use @BeforeMethod
instead of @BeforeTest
, since Spring's dependency injection occurs in an @BeforeClass method.
我同意 Cedric:使用@BeforeMethod
而不是@BeforeTest
,因为 Spring 的依赖注入发生在 @BeforeClass 方法中。
- Sam (author of the Spring TestContext Framework ;) )
- Sam(Spring TestContext 框架的作者;))
回答by Ran Adler
Use @PostConstruct not @BeforeXXX
使用 @PostConstruct 而不是 @BeforeXXX
回答by Akshay
Check if you have spring-asm dependency as well. If you have one it will conflict with spring-core dependency. I removed the asm dependency and this worked for me.
检查您是否也有 spring-asm 依赖项。如果你有一个它会与 spring-core 依赖冲突。我删除了 asm 依赖项,这对我有用。