使用 TestNG 进行 Spring 依赖注入

发布于 2024-08-28 10:57:04 字数 350 浏览 10 评论 0原文

Spring 在这方面对 JUnit 提供了很好的支持: 使用 RunWithContextConfiguration 注释,事情看起来非常直观

@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 技术交流群。

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

发布评论

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

评论(3

避讳 2024-09-04 10:57:04

这是一个对我有用的例子:

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

@Test
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestValidation extends AbstractTestNGSpringContextTests {

    public void testNullParamValidation() {
        // Testing code goes here!
    }
}

Here is an example that worked for me:

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

@Test
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestValidation extends AbstractTestNGSpringContextTests {

    public void testNullParamValidation() {
        // Testing code goes here!
    }
}
寄风 2024-09-04 10:57:04

Spring 和 TestNG 可以很好地配合使用,但有一些事情需要注意。除了子类化 AbstractTestNGSpringContextTests 之外,您还需要了解它如何与标准 TestNG 设置/拆卸注释交互。

TestNG 有四个级别的设置

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod

完全按照您的预期发生(自记录 API 的一个很好的例子)。这些都有一个名为 dependsOnMethods 的可选值,它可以采用 String 或 String[],这是同一级别的方法的名称。

AbstractTestNGSpringContextTests 类有一个名为 springTestContextPrepareTestInstanceBeforeClass 带注释的方法,如果您在中使用自动装配类,则必须将设置方法设置为依赖于该方法。它。对于方法,您不必担心自动装配,因为它是在类方法之前设置测试类时发生的。

这就留下了如何在用 BeforeSuite 注释的方法中使用自动装配类的问题。您可以通过手动调用 springTestContextPrepareTestInstance 来完成此操作 - 虽然默认情况下没有设置这样做,但我已经成功完成了几次。

因此,为了说明这一点,Arup 示例的修改版本:

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

@Test
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestValidation extends AbstractTestNGSpringContextTests {

    @Autowired
    private IAutowiredService autowiredService;

    @BeforeClass(dependsOnMethods={"springTestContextPrepareTestInstance"})
    public void setupParamValidation(){
        // Test class setup code with autowired classes goes here
    }

    @Test
    public void testNullParamValidation() {
        // Testing code goes here!
    }
}

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

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod

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 a BeforeClass annotated method called springTestContextPrepareTestInstance, 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 calling springTestContextPrepareTestInstance - 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:

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

@Test
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestValidation extends AbstractTestNGSpringContextTests {

    @Autowired
    private IAutowiredService autowiredService;

    @BeforeClass(dependsOnMethods={"springTestContextPrepareTestInstance"})
    public void setupParamValidation(){
        // Test class setup code with autowired classes goes here
    }

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