當我寫在JUnit測試(在Spring上下文中)我usualy做這樣的:是否TestNG的亞軍已經像基於SpringJUnit4ClassRunner
@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)
,這就是爲什麼我問亞軍。
http://stackoverflow.com/questions/2608528/spring-dependency-injection-with- testng – 2010-04-12 21:14:48
看起來像,但很不相同。 – 2010-04-12 21:41:24