views:

275

answers:

2

Hi,

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?

A: 

http://stackoverflow.com/questions/2608528/spring-dependency-injection-with-testng

TestNG does not have runners like JUnit.

lexicore
Sorry. I really did search first. I tried extend by `AbstractTestNGSpringContextTests` byt it didn't work. I'll try one more time, maybe I messd up something.
Michał Mech
Elaborate on "didn't work".
lexicore
A: 

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"&gt;

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

Michał Mech