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

Mic*_*ech 31 java junit testng spring

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

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

    @Test
    public void testMethod() {
        // execute test logic...
    }
}
Run Code Online (Sandbox Code Playgroud)

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


我会添加更多细节.使用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
}
Run Code Online (Sandbox Code Playgroud)

和描述符......

<?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>
Run Code Online (Sandbox Code Playgroud)

结果是......

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

Results :

Failed tests: 
  testMethod(pl.michalmech.ExampleTest)
Run Code Online (Sandbox Code Playgroud)

这就是我问跑步者的原因.

abi*_*net 3

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