【问题标题】:Spring Dependency Injection with TestNG使用 TestNG 进行 Spring 依赖注入
【发布时间】:2011-02-06 04:51:41
【问题描述】:

Spring 很好地支持 JUnit: 使用RunWithContextConfiguration 注释,看起来非常直观

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:dao-context.xml")

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

【问题讨论】:

    标签: junit spring testng


    【解决方案1】:

    【讨论】:

    • 谢谢。这正是我正在寻找的。​​span>
    • 真是一团糟。首先强制执行特定的类层次结构。其次是相当混乱,因为使用@Transactional 的测试用例可能会错误地扩展非事务版本。但不幸的是,没有其他方法可以将 Spring 与 TestNG 一起使用。
    • @GrzesiekD。我希望在 4.5 年内有些事情可能会发生变化。 :) 所以请重新检查现状。
    • 第一个对我不起作用,但第二个没问题。那么有和没有事务有什么区别呢?
    【解决方案2】:

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

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

    【讨论】:

    • @Test 类的注释? :o
    【解决方案3】:

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

    TestNG 有四个级别的设置

    • BeforeSuite
    • 测试前
    • 课前
    • 之前的方法

    这完全符合您的预期(自记录 API 的绝佳示例)。这些都有一个名为dependsOnMethods 的可选值,它可以采用String 或String[],这是同一级别的方法的名称。

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

    这只是留下了一个问题,即如何在使用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!
        }
    }
    

    【讨论】:

    • 方法 org.springframework.test.context.testng.AbstractTestNGSpringContextTests#springTestContextPrepareTestInstance 已经有注释 @BeforeClass 所以这个解决方案在我看来是多余的。
    • 此解决方案允许您向测试中添加与自动连线字段相关的代码。 “springTestContextPrepareTestInstance”作为“类前”方法并不能保证它会在子类的“前类”之前运行——您需要显式设置dependOnMethods字段
    • 不幸的是,这对我不起作用。默认情况下,@Autowire 似乎发生得很晚,在@BeforeTest 之后(但在@Test 之前)。我尝试添加dependsOnMethods,但后来我得到:MyClass 取决于方法受保护的void org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance() 抛出java.lang.Exception,它没有用@Test 注释...
    • 编辑:刚刚找到一个解决方案(问题是 BeforeSuite 不适用于此)。检查:stackoverflow.com/questions/5192562/…
    猜你喜欢
    • 2015-03-01
    • 2014-10-27
    • 2011-08-04
    • 2012-01-24
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    相关资源
    最近更新 更多