java TestNG 是否有像 SpringJUnit4ClassRunner 这样的跑步者

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

Does TestNG has runner like SpringJUnit4ClassRunner

javaspringjunittestng

提问by Micha? Mech

When I write tests in JUnit (in Spring context) I usualy do it like this:

当我在 JUnit 中(在 Spring 上下文中)编写测试时,我通常这样做:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:testContext.xml")
public class SimpleTest {

    @Test
    public void testMethod() {
        // execute test logic...
    }
}

How can I do the same with TestNG?

我怎样才能用 TestNG 做同样的事情?



I'll add more details. With AbstractTestNGSpringContextTests it works, but not in a way I want to. I have some test ...

我会添加更多细节。使用 AbstractTestNGSpringContextTests 它可以工作,但不是我想要的方式。我有一些测试...

@ContextConfiguration(locations = { "classpath:applicationContextForTests.xml" })
public class ExampleTest extends AbstractTestNGSpringContextTests {

    private Boolean someField;

    @Autowired
    private Boolean someBoolean;

    @Test
    public void testMethod() {
        System.out.println(someField);
        Assert.assertTrue(someField);
    }

    @Test
    public void testMethodWithInjected() {
        System.out.println(someBoolean);
        Assert.assertTrue(someBoolean);
    }

    // setters&getters
}

and descriptor ...

和描述符...

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="exampleTest" class="pl.michalmech.ExampleTest">
        <property name="someField">
            <ref bean="someBoolean"/>
        </property>
    </bean>

    <bean id="someBoolean" class="java.lang.Boolean">
        <constructor-arg type="java.lang.String" value="true"/>
    </bean>
</beans>

The results are ...

结果是...

null
true
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.599 sec <<< FAILURE!

Results :

Failed tests: 
  testMethod(pl.michalmech.ExampleTest)

That's why I asked about runner.

这就是我询问runner的原因。

回答by abinet

TestNG does not use Spring to instantiate your test. That's why someField=null

TestNG 不使用 Spring 来实例化您的测试。这就是为什么 someField=null

回答by Klaus Groenbaek

Correct, TestNG always instantiates the Test class (put breakpoint in constructor to verify). Later (@BeforeClass) the beans from the context is injected into the Test class.

正确,TestNG 总是实例化 Test 类(在构造函数中放置断点以进行验证)。稍后(@BeforeClass)来自上下文的 bean 被注入到 Test 类中。

I'm however curious as to why you would every define the test as a bean in the first place. In the 10 years I have used Spring I have never needed to do that, or seen anyone do it...

但是,我很好奇为什么您首先将测试定义为 bean。在我使用 Spring 的 10 年中,我从未需要这样做,也从未见过任何人这样做......