当我在 JUnit 中(在 Spring 上下文中)编写测试时,我通常这样做:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:testContext.xml")
public class SimpleTest {
@Test
public void testMethod() {
// execute test logic...
}
}
我怎样才能对 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
}
和描述符...
<?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>
结果是...
null
true
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.599 sec <<< FAILURE!
Results :
Failed tests:
testMethod(pl.michalmech.ExampleTest)
这就是我问runner的原因。