使用 TestNG 进行 Spring 依赖注入
Spring 在这方面对 JUnit 提供了很好的支持: 使用 RunWith
和 ContextConfiguration
注释,事情看起来非常直观
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:dao-context.xml")
这个测试将能够在 Eclipse 和 Eclipse 中运行。 Maven 正确。 我想知道 TestNG 是否有类似的东西。我正在考虑转向这个“下一代”框架,但我没有找到与 Spring 测试相匹配的框架。
Spring support JUnit quite well on that:
With the RunWith
and ContextConfiguration
annotation, things look very intuitive
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:dao-context.xml")
This test will be able to run both in Eclipse & Maven in correctly.
I wonder if there is similar stuff for TestNG. I'm considering moving to this "Next Generation" Framework but I didn't find a match for testing with Spring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它也适用于 TestNG。您的测试类需要扩展以下类之一:
org.springframework.test.context.testng.AbstractTestNGSpringContextTests
org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
It works with TestNG as well. Your test class needs to extend one of the following classes:
org.springframework.test.context.testng.AbstractTestNGSpringContextTests
org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
这是一个对我有用的例子:
Here is an example that worked for me:
Spring 和 TestNG 可以很好地配合使用,但有一些事情需要注意。除了子类化
AbstractTestNGSpringContextTests
之外,您还需要了解它如何与标准 TestNG 设置/拆卸注释交互。TestNG 有四个级别的设置
完全按照您的预期发生(自记录 API 的一个很好的例子)。这些都有一个名为
dependsOnMethods
的可选值,它可以采用 String 或 String[],这是同一级别的方法的名称。AbstractTestNGSpringContextTests
类有一个名为springTestContextPrepareTestInstance
的BeforeClass
带注释的方法,如果您在中使用自动装配类,则必须将设置方法设置为依赖于该方法。它。对于方法,您不必担心自动装配,因为它是在类方法之前设置测试类时发生的。这就留下了如何在用
BeforeSuite
注释的方法中使用自动装配类的问题。您可以通过手动调用 springTestContextPrepareTestInstance 来完成此操作 - 虽然默认情况下没有设置这样做,但我已经成功完成了几次。因此,为了说明这一点,Arup 示例的修改版本:
Spring and TestNG work well together, but there are some things to be aware of. Aside from subclassing
AbstractTestNGSpringContextTests
, you need to be aware of how it interacts with standard TestNG setup/teardown annotations.TestNG has four levels of setup
which occur exactly as you would expect (great example of self-documenting APIs). These all have an optional value called
dependsOnMethods
which can take a String or String[], which is the name or name(s) of the methods at the same level.The
AbstractTestNGSpringContextTests
class has aBeforeClass
annotated method calledspringTestContextPrepareTestInstance
, which you must set your setup method to depend on if you are using an autowired class in it. For methods, you don't have to worry about the autowiring, since it occurs when the test class is setup in that before class method.This just leaves the question of how you might use an autowired class in a method annotated with
BeforeSuite
. You can do this by manually callingspringTestContextPrepareTestInstance
- while its not setup by default to do this, I've done it several times successfully.So, to illustrate, a modified version of Arup's example: