iteye_6838 2009-06-01 10:49
浏览 290
已采纳

单元测试中,setter属性的类型无法转换成功

[quote][/quote]开发环境:eclipse 3.2
框架 spring + ibatis + dwr

单元测试时候提示的错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MsuserService' defined in file [D:\workspace\MS2\src\org\ag\ms\test\WEB-INF\applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0] to required type [org.ag.ms.dao.impl.MsuserDaoImpl] for property 'msuserDao'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0] to required type [org.ag.ms.dao.impl.MsuserDaoImpl] for property 'msuserDao': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1247)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:986)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:238)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:167)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:235)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:167)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:385)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:730)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:93)
at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:77)
at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:67)
at org.ag.ms.test.AgTest.setUp(AgTest.java:14)
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.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
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)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0] to required type [org.ag.ms.dao.impl.MsuserDaoImpl] for property 'msuserDao'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0] to required type [org.ag.ms.dao.impl.MsuserDaoImpl] for property 'msuserDao': no matching editors or conversion strategy found
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:104)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1244)
... 26 more

首先:spring的applicationContext.xml的内容如下:



src/org/ag/ms/test/WEB-INF/datasourceConfig.properties


class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close" lazy-init="default" autowire="default"
dependency-check="default">









<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation" value="classpath:org/ag/ms/xml/sqlMapConfig.xml"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
        <props>
            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="insert*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
</bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
        <value>*Impl</value>
    </property>
    <property name="interceptorNames">
        <list>
            <value>transactionInterceptor</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
    <property name="transactionInterceptor"
        ref="transactionInterceptor" />
</bean>


[color=red]<bean id="MsuserDaoImpl" class="org.ag.ms.dao.impl.MsuserDaoImpl">
    <property name="sqlMapClient" ref="sqlMapClient"/>
</bean>

<bean id="MsuserService" class="org.ag.ms.service.impl.MsuserImpl">
    <property name="msuserDao" ref="MsuserDaoImpl"></property>
</bean>[/color]

单元测试:
import java.util.List;

import junit.framework.TestCase;

import org.ag.ms.dao.MsuserDao;
import org.ag.ms.model.TMsuser;

public class AgTest extends TestCase {
private org.springframework.context.ApplicationContext ac;
public void setUp(){
String applcationFile = "src/org/ag/ms/test/WEB-INF/applicationContext.xml";
ac = new org.springframework.context.support.FileSystemXmlApplicationContext(applcationFile);
}

public void testSpringBean(){
    MsuserDao mui = (MsuserDao)ac.getBean("MsuserDaoImpl");
              TMsuser tm = (TMsuser)mui.selectMsuserById(1);
    List list = (List) mui.selectMsuserByCell("13000000004");
    List list2 = (List) mui.selectMsuserByNickname("huanyi4");
    System.out.println(list.size());
    System.out.println(list2.size());
    System.out.println(tm.getClass());
    System.out.println(tm.getDestination());
    }

}

没有写入:

[color=red]的时候的单元测试时通过的,加了这个bean就错误[/color]

对应的类:
package org.ag.ms.service.impl;

import java.util.List;

import org.ag.ms.dao.impl.MsuserDaoImpl;
import org.ag.ms.service.inter.MsuserInter;

public class MsuserImpl implements MsuserInter {
private MsuserDaoImpl [color=red]msuserDao[/color];
public boolean deleteMsuserById(Integer id) {
// TODO Auto-generated method stub
return false;
}
. ......
public void setMsuserDao(MsuserDaoImpl msuserDao){
this.msuserDao = msuserDao;
}
public MsuserDaoImpl getMsuserDao(){
return msuserDao;
}
}

[b]问题补充:[/b]
万分感谢layer555高手的指点,果然在spring里头需要用接口而不是用实现类。可以在dwr的测试页面中看到了该对象,而且还有对应的方法,但是又遇到新问题,就是查询出来的结果是null,测试的报错是:
[color=red]java.lang.NullPointerException[/color]
at org.ag.ms.test.AgTest.testSpringBean(AgTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
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:128)
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)

  • 写回答

2条回答 默认 最新

  • layer555 2009-06-02 10:13
    关注

    晕。。。刚开始还以为是事务的原因,后来发现MsuserImpl中的msuserDao属性怎么能是impl类型呢?应该用接口,不是MsuserDaoImpl而应该是MsuserDao;因为Spring会为有事务的Service生成相应的proxy,默认是针对接口生成的,那么$Proxy其实应该是MsuserDao的代理对象。所以才出现了$Proxy不能被转换成MsuserDaoImpl的错误。你改下试试应该是这个原因。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式