Spring 单元测试遇到的问题
今天参照AppFuse1.9.4的框架结构自己动手做了个小例了,但是在集成sping + hibernate时,GenericDaoTest测试通过了,但TUserDaoTest单元测试始终过不去,报错:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.ccsee.portal.dao.TUserDaoTest': Unsatisfied dependency expressed through bean property 'permissiondao': 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)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
下面贴出源码,请高手给指点一二:
package com.ccsee.portal.dao;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
public abstract class BaseDaoTestCase extends AbstractTransactionalDataSourceSpringContextTests {
protected final Log log = LogFactory.getLog(getClass());
protected ResourceBundle rb;
@Override
protected String[] getConfigLocations() {
setAutowireMode(AUTOWIRE_BY_NAME);
return new String[]{"classpath*:/**/dao/applicationContext-resources.xml",
"classpath*:META-INF/applicationContext-*.xml"};
}
public BaseDaoTestCase() {
String className = this.getClass().getName();
try{
rb = ResourceBundle.getBundle(className);
}catch(MissingResourceException mrex){
log.warn("OK, no resource bundle for '" + className + "'");
}
}
protected Object populate(Object obj) throws Exception{
Map map = new HashMap();
for(Enumeration keys = rb.getKeys(); keys.hasMoreElements();){
String key = (String)keys.nextElement();
map.put(key, rb.getString(key));
}
return obj;
}
}
package com.ccsee.portal.dao;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.orm.ObjectRetrievalFailureException;
import com.ccsee.portal.model.TUser;
public class GenericDaoTest extends BaseDaoTestCase {
protected Dao dao;
public void onSetUpBeforeTransaction() throws Exception{
dao = (Dao)applicationContext.getBean("dao");
}
public void onTearDownAfterTransaction() throws Exception{
dao = null;
}
public void testCRUD(){
TUser user = new TUser();
user.setUsername("zph");
user.setPassword("123456");
user.getAddress().setAddress("北京市海淀区中关村东路123号都市网景B座1603A");
user.getAddress().setPostCode("1000880");
user.setEmail("loup386@163.com");
user.setAccountExpired(false);
user.setAccountLocked(false);
user.setAccountEnable(true);
//Create Test
dao.saveObject(user);
assertNotNull(user.getId());
log.info("Test save over");
//Retrieve Test
user = (TUser)dao.getObject(TUser.class, user.getId());
assertNotNull(user);
assertEquals(user.getUsername(), "zph");
//Update Test
user.getAddress().setCountry("China");
dao.saveObject(user);
assertEquals(user.getAddress().getCountry(), "China");
//Delete Test
dao.removeObject(TUser.class, user.getId());
try{
dao.getObject(TUser.class, user.getId());
fail("User 'zph' not found in database");
}catch(ObjectRetrievalFailureException orfex){
assertNotNull(orfex.getMessage());
}catch(InvalidDataAccessApiUsageException idaauex){
assertNotNull(idaauex.getMessage());
}
}
}
package com.ccsee.portal.dao;
import com.ccsee.portal.model.Address;
import com.ccsee.portal.model.TUser;
public class TUserDaoTest extends BaseDaoTestCase {
private TUserDao userdao = null;
private TRoleDao roledao = null;
private TPermissionDao permissiondao = null;
private TResourceDao resourcedao = null;
public void setUserdao(TUserDao userdao) {
this.userdao = userdao;
}
public void setRoledao(TRoleDao roledao) {
this.roledao = roledao;
}
public void setPermissiondao(TPermissionDao permissiondao) {
this.permissiondao = permissiondao;
}
public void setResourcedao(TResourceDao resourcedao) {
this.resourcedao = resourcedao;
}
public void testSaveTUser() {
TUser user = new TUser();
user.setUsername("testuser");
user.setPassword("testpass");
user.setName("Test");
Address address = new Address();
address.setCity("BeiJing");
address.setProvince("BeiJing");
address.setCountry("China");
address.setPostCode("80210");
user.setAddress(address);
user.setEmail("loup386@163.com");
user.setWebsite("http://www.ccsee.com");
userdao.saveTUser(user);
assertNotNull(user.getId());
assertEquals("testpass", user.getPassword());
}
}
1 楼 EXvision 2008-06-03 Error creating bean with name 'com.ccsee.portal.dao.TUserDaoTest': Unsatisfied dependency expressed through bean property 'permissiondao': Set this property value or disable dependency checking for this bean.
是IoC的问题,检查你的配置文件。permissiondao的配置。
貌似错误信息非常明显了。