使用 maven-surefire 运行测试时,Spring-Autowiring 在 @BeforeClass 之后发生

发布于 2024-10-20 10:16:51 字数 1423 浏览 0 评论 0原文

我在依赖注入(Spring 自动装配)和 maven-surefire 方面遇到一些问题。 当使用 TestNG 在 eclipse 中运行时,以下测试可以正常工作: 注入服务对象,然后调用@BeforeClass方法。

@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);
}

但是,当我使用 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].

我该如何解决这个问题? 如果我用 @Test 替换 @BeforeClass ,它可以在 Maven 中工作,就像在 TestNG 的 Eclipse 插件中一样。

maven-surefire-plugin:2.7.2

Eclipse:Helios 服务版本 1

jdk1.6.0_14

TestNG:5.14.10

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.

@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:

[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.

maven-surefire-plugin:2.7.2

Eclipse: Helios Service Release 1

jdk1.6.0_14

TestNG: 5.14.10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

謸气贵蔟 2024-10-27 10:16:51

此外,在此问题得到解决之前,如果按照之前的建议后仍然无法正常工作,或者您如果您不希望您的代码在每个方法之前执行,那么请将以下代码添加到您的测试类中:

@Override
@BeforeSuite
protected void springTestContextPrepareTestInstance() throws Exception {
    super.springTestContextPrepareTestInstance();
}

这可确保在执行您的 @BeforeClass 方法之前准备好 Spring Context。

*注意,我发布了这个答案,因为在标题中您询问了 @BeforeClass,即使您的示例代码中没有使用 @BeforeClass。

Additionally, until this issue is 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.

*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.

甜味拾荒者 2024-10-27 10:16:51

使用@BeforeMethod,而不是@BeforeTest。

Use @BeforeMethod, not @BeforeTest.

罪#恶を代价 2024-10-27 10:16:51

我同意 Cedric 的观点:使用 @BeforeMethod 而不是 @BeforeTest,因为 Spring 的依赖注入发生在 @BeforeClass 方法中。

  • Sam(Spring TestContext 框架的作者;))

I agree with Cedric: use @BeforeMethod instead of @BeforeTest, since Spring's dependency injection occurs in an @BeforeClass method.

  • Sam (author of the Spring TestContext Framework ;) )
虫児飞 2024-10-27 10:16:51

使用@PostConstruct而不是@BeforeXXX

Use @PostConstruct not @BeforeXXX

夜无邪 2024-10-27 10:16:51

检查您是否也有 spring-asm 依赖项。如果你有一个它将与 spring-core 依赖冲突。我删除了 asm 依赖项,这对我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文