开发者

Does TestNG has runner like SpringJUnit4ClassRunner

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

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


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

@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" 开发者_Go百科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.


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


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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜