JUnit期间的ClassCastException

时间:2013-07-09 12:42:37

标签: java junit

这是我的代码

Query query = getEntityManager().createQuery("from " + Applicazione.class.getName() +" a where a.id = :id");
    query.setParameter("id", id);
    Applicazione applicazione = (Applicazione) query.getSingleResult();

这很简单。但是我不明白为什么在JUnit Test期间getSingleResult()方法会引发一个转发异常......

  • 相同的上下文(更多上下文相同的类可以提供此ex)
  • 检查SerialID

为什么会发生?!?!

由于

更新 这是堆栈跟踪

java.lang.ClassCastException: 
it.xxxxx.intranet.core.model.Applicazione cannot be cast to 
it.xxxxx.intranet.core.model.Applicazione

java.lang.ClassCastException: it.XXXX.intranet.core.model.Applicazione cannot be cast to it.XXXX.intranet.core.model.Applicazione
    at it.xxxxx.intranet.core.dao.impl.ApplicazioneDaoImpl.findApplicazioneById(ApplicazioneDaoImpl.java:40)
    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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy16.findApplicazioneById(Unknown Source)
    at it.xxxxx.intranet.core.service.impl.ApplicazioneServiceImpl.findApplicazioneById(ApplicazioneServiceImpl.java:25)
    at it.xxxxx.intranet.core.test.GestioneApplicazioniTest.detailApplicazioneTest(GestioneApplicazioniTest.java:44)
    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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    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)

第40行:Applicazione applicazione = (Applicazione) query.getSingleResult();

1 个答案:

答案 0 :(得分:1)

您的班级中似乎导入了错误的Applicazione(因为您的班级名称相同,我假设他们在不同的包中)。

检查import Applicazione语句并修复它。

修改

我刚刚发现你正在使用"from " + Applicazione.class.getName() +" a where a.id = :id"字符串。因此,如果您的查询返回结果,即使分配应该工作(没有异常) 因为您不能为具有相同名称的2个不同包类指定简单名称,并且其中一个应具有完全限定的类名。你确定你发布了正确的代码吗?