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