使用 TestNG 进行 Spring 依赖注入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2608528/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:02:34  来源:igfitidea点击:

Spring Dependency Injection with TestNG

junitspringtestng

提问by Ph??ng Nguy?n

Spring support JUnit quite well on that: With the RunWithand ContextConfigurationannotation, things look very intuitive

Spring 对 JUnit 的支持非常好:使用RunWithContextConfiguration注释,事情看起来非常直观

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

此测试将能够在 Eclipse 和 Maven 中正确运行。我想知道TestNG是否有类似的东西。我正在考虑转向这个“下一代”框架,但我没有找到与 Spring 测试的匹配项。

回答by Arup Malakar

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!
    }
}

回答by romeara

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.

Spring 和 TestNG 可以很好地协同工作,但有一些事情需要注意。除了继承 AbstractTestNGSpringContextTests 之外,您还需要了解它如何与标准 TestNG 设置/拆卸注释进行交互。

TestNG has four levels of setup

TestNG 有四个级别的设置

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

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

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.

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

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.

这只是留下了如何在使用 BeforeSuite 注释的方法中使用自动装配类的问题。您可以通过手动调用 springTestContextPrepareTestInstance 来完成此操作 - 虽然默认情况下未设置为执行此操作,但我已成功完成多次。

So, to illustrate, a modified version of Arup's example:

因此,为了说明,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!
    }
}