古人学问无遗力,少壮功夫老始成。 纸上得来终觉浅,绝知此事要躬行。
【原创】写JUnit时既用Parameterized又用@Autowired出现无法成功自动注入的解决办法
上一篇 /
下一篇 2011-07-18 17:51:29
/ 个人分类:测试工具
1、问题现象:
………………………………
@RunWith(value=Parameterized.class)
public class TestRegisterEventMaker extends BaseSpringContextTestSupport {
………………………………………………
@Autowired
private SponsorDaoImpl sponsorDao;
private long sponsorId;
private String name;
private int type;
private int excepted;
………………………………………………
@Parameters
public static Collection registerData() {
return Arrays.asList(new Object[][]{
{1L,"kevin",1,1}
});
public TestRegisterEventMaker(long sponsorId,String name,int type,int excepted)
{
this.sponsorId = sponsorId;
this.name = name;
this.type = type;
this.excepted = excepted;
}
………………………………………………
@Test
public void testGenerateDataSponsor() throws Exception {
………………………………………………
}
}
………………………………………………(此处省略N行代码)
- sponsorDao一直为null。。。。。。。 T_TPS:没使用Parameterized之前,还是可以成功注入的
2、问题解决
………………………………
@RunWith(value=Parameterized.class)
public class TestRegisterEventMaker extends BaseSpringContextTestSupport {
………………………………………………
………………………………………………
@Autowired
private SponsorDaoImpl sponsorDao;
private TestContextManager testContextManager;
private long sponsorId;
private String name;
private int type;
private int excepted;
………………………………………………
@Parameters
public static Collection registerData() {
return Arrays.asList(new Object[][]{
{1L,"kevin",1,1}
});
public TestRegisterEventMaker(long sponsorId,String name,int type,int excepted)
{
this.sponsorId = sponsorId;
this.name = name;
this.type = type;
this.excepted = excepted;
}
………………………………………………
@Test
public void testGenerateDataSponsor() throws Exception {
this.testContextManager = new TestContextManager(getClass());
//需要try...catch的,注意(或直接像我一样Exception)
this.testContextManager.prepareTestInstance(this);
………………………………………………
}
}
………………………………………………(此处省略N行代码)
收藏
举报
TAG:
JUnit
Junit
JUNIT
junit
Parameterized
Autowired
自动注入