Topic: [求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误

  Print this page

1.[求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误 Copy to clipboard
Posted by: down4u
Posted on: 2005-10-31 15:59

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.appfuse.dao.PersonDAOTest' defined in null: Unsatisfied dependency expressed through bean property 'personDAO': set this property value or disable dependency checking for this bean
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:961)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:822)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:200)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:138)
at junit.framework.TestCase.runBare(TestCase.java:125)
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.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

不知道是因何而起?

2.Re:[求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误 [Re: down4u] Copy to clipboard
Posted by: floater
Posted on: 2005-10-31 22:20

It's the first line:

Error creating bean with name 'org.appfuse.dao.PersonDAOTest' defined in null: Unsatisfied dependency expressed through bean property 'personDAO': set this property value or disable dependency checking for this bean

It looks like you testcase has a property: personDAO and has dep checking enabled. Spring can't set this property and thus complains. make sure you have a setter for this property in your testcase and you have a valid ref to set this.

3.Re:[求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误 [Re: floater] Copy to clipboard
Posted by: down4u
Posted on: 2005-11-01 11:31

floater wrote:
It's the first line:

It looks like you testcase has a property: personDAO and has dep checking enabled. Spring can't set this property and thus complains. make sure you have a setter for this property in your testcase and you have a valid ref to set this.


检查了没问题啊,master看得出问题在哪里吗?

PersonDAOTest.java
package org.appfuse.dao;

import org.appfuse.model.Person;

public class PersonDAOTest extends BaseDAOTestCase {

private Person person = null;
private PersonDAO dao = null;

public void setPersonDAO(PersonDAO dao) {
this.dao = dao;
}

public void testGetPerson() throws Exception {
person = new Person();
person.setFirstName("Matt");
person.setLastName("Raible");

dao.savePerson(person);
}
}


BaseDAOTestCase.java
package org.appfuse.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.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

/**
* Base class for running DAO tests.
* @author mraible
*/
public abstract class BaseDAOTestCase extends AbstractDependencyInjectionSpringContextTests {
protected final Log log = LogFactory.getLog(getClass());
protected ResourceBundle rb;

protected String[] getConfigLocations() {
return new String [] {"classpath*:/WEB-INF/applicationContext.xml"};
}

public BaseDAOTestCase() {
// Since a ResourceBundle is not required for each class, just
// do a simple check to see if one exists
String className = this.getClass().getName();

try {
rb = ResourceBundle.getBundle(className);
} catch (MissingResourceException mre) {
//log.warn("No resource bundle found for: " + className);
}
}

/**
* Utility method to populate a javabean-style object with values
* from a Properties file
* @param obj
* @return
* @throws Exception
*/
protected Object populate(Object obj) throws Exception {
// loop through all the beans methods and set its properties from
// its .properties file
Map map = new HashMap();

for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
String key = (String) keys.nextElement();
map.put(key, rb.getString(key));
}

BeanUtils.copyProperties(obj, map);

return obj;
}
}


applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/config.properties</value>
</property>
</bean>

<!-- DataSoure -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${hibernate.connection.driver_class}</value></property>
<property name="url"><value>${hibernate.connection.url}</value></property>
<property name="username"><value>${hibernate.connection.username}</value></property>
<property name="password"><value>${hibernate.connection.password}</value></property>
<property name="maxActive"><value>${hibernate.connection.maxActive}</value></property>
<property name="maxWait"><value>${hibernate.connection.maxWait}</value></property>
<property name="maxIdle"><value>${hibernate.connection.maxIdle}</value></property>
</bean>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>org/appfuse/model/Person.hbm.xml</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
</props>
</property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- Generic DAO - can be used when doing standard CRUD -->
<bean id="dao" class="org.appfuse.dao.hibernate.BaseDAOHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- PersonDAO: Hibernate implementation -->
<bean id="PersonDAO" class="org.appfuse.dao.hibernate.PersonDAOHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- Add new DAOs here -->

</beans>

4.Re:[求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误 [Re: down4u] Copy to clipboard
Posted by: floater
Posted on: 2005-11-01 11:38

where is the line to set the dao(calling your setter)?

5.Re:[求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误 [Re: floater] Copy to clipboard
Posted by: down4u
Posted on: 2005-11-01 13:18

floater wrote:
where is the line to set the dao(calling your setter)?


不是这样的吗?这是照着appfuse的文档做的

小可初学,望master教我

6.Re:[求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误 [Re: down4u] Copy to clipboard
Posted by: floater
Posted on: 2005-11-02 11:05

hmm, I didn't notice you are using spring's testcase class. change your dao declaration to protected, not using private.

7.Re:[求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误 [Re: floater] Copy to clipboard
Posted by: down4u
Posted on: 2005-11-02 11:36

floater wrote:
hmm, I didn't notice you are using spring's testcase class. change your dao declaration to protected, not using private.


org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.appfuse.dao.PersonDAOTest' defined in null: Unsatisfied dependency expressed through bean property 'personDAO': set this property value or disable dependency checking for this bean
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:961)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:822)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:200)
  at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:138)
  at junit.framework.TestCase.runBare(TestCase.java:125)
  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.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

8.Re:[求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误 [Re: down4u] Copy to clipboard
Posted by: floater
Posted on: 2005-11-04 21:49

You testcase extends Matt's testcase, which extends Spring's testcase. This means that your dao class in your testcase will be injected by Spring(unless you want to manually inject it). Spring's super class will look for all protected members and inject them from app context. It doesn't check private members. So in you testcase, you should declare dao as protected, rather than private.

9.Re:[求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误 [Re: floater] Copy to clipboard
Posted by: down4u
Posted on: 2005-11-05 12:09

floater wrote:
You testcase extends Matt's testcase, which extends Spring's testcase. This means that your dao class in your testcase will be injected by Spring(unless you want to manually inject it). Spring's super class will look for all protected members and inject them from app context. It doesn't check private members. So in you testcase, you should declare dao as protected, rather than private.


private PersonDAO dao = null;改为protected PersonDAO dao = null;
可问题依旧,哎...


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923