浏览 3193 次
|
锁定老贴子 主题:Spring单元测试中两种类型的测试都报错
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2006-11-23 关键字: 单元测试
首先我测试DAO的时候我写了一个测试父类:
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
public class AbstractTestDao extends
AbstractTransactionalDataSourceSpringContextTests {
@Override
protected String[] getConfigLocations() {
return new String[] {"classpath*:/etc/spring/*.xml"};
}
}
然后我写一个需要测试的子类继承它: public class PhotoInfoDaoImplTest extends AbstractTestDao {
private PhotoInfoDaoImpl photoInfoDao;
protected void onSetUpInTransaction() throws Exception {
super.onSetUpInTransaction();
//this.setPopulateProtectedVariables(true);
photoInfoDao = (PhotoInfoDaoImpl) this.applicationContext.getBean("photoInfoDao");
}
public void onTearDownAfterTransaction() throws Exception {
photoInfoDao = null;
}
public void testUpdatePhotoInfos() {
String[] ids = {"1","2","3"};
Long albumId = Long.valueOf(1);
this.photoInfoDao.updatePhotoInfos(ids, albumId);
PhotoInfo photoInfo = (PhotoInfo)this.photoInfoDao.getObject(PhotoInfo.class, 2);
Assert.assertEquals(photoInfo.getAlbumInfo().getAlbumId()==1, true);
}
}
需要测试的DAO类定义如下: public class PhotoInfoDaoImpl extends BaseDAOHibernateImpl implements PhotoInfoDao 但运行这个测试方法的时候报错如下: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.test.dao.PhotoInfoDaoImplTest': Unsatisfied dependency expressed through bean property 'dataSource': Set this property value or disable dependency checking for this bean. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:923) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:728) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:266) at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:179) at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:158) at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:76) at junit.framework.TestCase.runBare(TestCase.java:125) at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69) 我继承AbstractTransactionalSpringContextTests类测试Serivce类的时候也报错,只不过换了一种,报的是Unsatisfied dependency expressed through bean property 'transactionManger'的错误..仔细对照了代码,也在网上搜索了一下,发现大家写法都一样,为什么我的就会出问题呢?applicationContext.xml 的配置也没有问题 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2006-11-23
默认情况下spring mock父类会通过autowire-byType来注入依赖关系。
可能你的Dao不是通过set方法注入给TestCase的,而是你自己getBean得到了。所以该Dao没有得到transactionManager的注入。 |
|
| 返回顶楼 | |
|
最后更新时间:2006-11-24
robbin 写道 默认情况下spring mock父类会通过autowire-byType来注入依赖关系。
可能你的Dao不是通过set方法注入给TestCase的,而是你自己getBean得到了。所以该Dao没有得到transactionManager的注入。 问题解决了,但答案却让人很奇怪,难道是Spring的BUG??? 我在测试的时候只能把Spring的xml配置文件放到项目src下测试才行与src同级的etc都不行.. 也就是只能写成这样 protected String[] getConfigLocations() {
return new String[] {"classpath:resources/spring/*.xml"};
}
如果换成把Spring的XML配置文件放到与src同级的etc/spring测试dao下面就会报dataSource找不到,测试service时就报transactionManger找不到,换到放项目src的resources/spring/下的xml就成功了,太郁闷了,难道是Spring测试WEB项目的BUG?? protected String[] getConfigLocations() {
return new String[] {"classpath*:/etc/spring/*.xml"};
}
|
|
| 返回顶楼 | |
|
最后更新时间:2006-11-24
etc是否没有包含在WAR包中?
|
|
| 返回顶楼 | |
|
最后更新时间:2006-11-24
没加到classpath下呗
你用的ant任务跑测试,还是在直接在eclipse里跑? |
|
| 返回顶楼 | |
|
最后更新时间:2006-11-24
daquan198163 写道 没加到classpath下呗
你用的ant任务跑测试,还是在直接在eclipse里跑? 直接在Eclipse里面跑的了..用了myeclipse.就懒了点 |
|
| 返回顶楼 | |
|
最后更新时间:2006-11-24
src是classpath etc是什么 没有特殊设置当然找不到了 随便一个文件夹下他都会去看的么
|
|
| 返回顶楼 | |
|
最后更新时间:2007-08-18
我的错误跟一楼的基本上一样,我是直接用AppFuse的那个BaseDaoTestCase(Aotowrite是byName)改了一下文件路径而已,而且我照他的方法将applicationContext.xml放在了src下也不行,但我觉得事实上跟这个应该没有问题的,因为只要文件是放在SourceFolder(Eclipse建的源代码目录)就应该OK的啊,而且我可以确定的是我配置文件没有错误中,不然也不会报这个异常了,还有更奇怪的是,我将AutoWrite设为no,然后get配置文件里随便一个Bean都为null,很奇怪全部都做得没有问题,但就是运行不了.每次都报这个错,郁闷了几天....
|
|
| 返回顶楼 | |
|
最后更新时间:2007-08-18
终于找出原因了,原因是因为我直接在配置文件中写jdbc url,而且里面出现了&字符,所以是个低级错误,希望不要有人再犯这种错误....
|
|
| 返回顶楼 | |











