java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.xxx.doma

用junit测试dao层和services层的时候,报出如下错误:

 

java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.xxx.domain.business.ModelTest
 at com.xxx.dao.business.ModelTestDAOImpl.selectModelTestDetailByPrimaryKey(ModelTestDAOImpl.java:197)
 at com.xxx.dao.business.ModelTestDAOImpl$$FastClassByCGLIB$$92b6c874.invoke(<generated>)
 at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
 at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
 at com.xxx.dao.business.ModelTestDAOImpl$$EnhancerByCGLIB$$5448ecda.selectModelTestDetailByPrimaryKey(<generated>)
 at com.xxx.services.business.ModelTestImpl.queryModelTest(ModelTestImpl.java:98)
 at com.test.services.business.TestModelTestImpl.saveModelTest(TestModelTestImpl.java:77)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:163)
 at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:233)
 at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:333)
 at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
 at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
 at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:142)
 at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
 at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
 at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
 at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
 at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

 

 

问题出在:

public class ModelTestDAOImpl extends SqlMapClientDaoSupport implements ModelTestDAO {

 

 @Override
 public ModelTest selectModelTestDetailByPrimaryKey( Long modelTestId ) {
  ModelTest key = new ModelTest();
  key.setModelTestId( modelTestId );  
  ModelTest record = (ModelTest)getSqlMapClientTemplate().queryForList( "Q_MODEL_TEST.ibatorgenerated_selectDetailByPrimaryKey", key );
  return record;
 }

 
}

 

queryForList 返回的是List,cannot be cast to ModelTest 把 queryForList 改为 queryForObject即可!

下次小心点~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值