How to deal with ClassCastException using Mokito

2,151 views
Skip to first unread message

akiMock

unread,
Apr 10, 2012, 7:24:24 AM4/10/12
to moc...@googlegroups.com
Hi,
         I am facing the class cast Exception issue in Testing. if anybody has already had faced this issue, please explain me how to deal with it using mockito.
 
Thanks'

Brice Dutheil

unread,
Apr 10, 2012, 7:45:13 AM4/10/12
to moc...@googlegroups.com
Hi,

Could you post the stack trace and some code sample ?

Cheers,

-- Brice

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/hb9JQOSJqMYJ.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.

akiMock

unread,
Apr 10, 2012, 8:10:04 AM4/10/12
to moc...@googlegroups.com
java.lang.ClassCastException: org.apache.axis.client.Stub$$EnhancerByMockitoWithCGLIB$$5f8f4bd9 incompatible with com.service.installmentLoanInformation.InstallmentLoanInformation
 at com.account.dao.impl.RspInstallmentLoanInformationWebServiceClientTest.testGetInstallmentLoanInfo(RspInstallmentLoanInformationWebServiceClientTest.java:33)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:618)
 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.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
 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.junit.runners.ParentRunner.run(ParentRunner.java:236)
 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)
 
 
 
part of the code
 
public SomeClassObject getStubData(string1, string2){
//
//
ProjectInformation service = (ProjectInformation) getStub().       // here the stub is the apache stub. you can get more info about it in err stack. at this line it is throwing the error.
//
...

Brice Dutheil

unread,
Apr 10, 2012, 8:45:20 AM4/10/12
to moc...@googlegroups.com
From the fragments of information you posted, I gather that :

a. You have a mock of the type org.apache.axis.client.Stub
b. The ClassCastException seems to originate from the test method : see line RspInstallmentLoanInformationWebServiceClientTest.testGetInstallmentLoanInfo(RspInstallmentLoanInformationWebServiceClientTest.java:33)
c. The code tries to cast the Axis type Stub to some other type in your code base com.service.installmentLoanInformation.InstallmentLoanInformation that is obviously not the same type or in the type hierarchy.


If however another exception is thrown from the snippet you posted then this statement looks the culprit to me :
(ProjectInformation) getStub()

Also this snippet doesn't reflects the type mentionned in the stacktrace, the stack trace is referring to some com.service.installmentLoanInformation.InstallmentLoanInformation while the code indicates the type ProjectInformation.

I never used Axis, I don't know what is the signature of getStub() or what it should return, but my guess is that you should investigate why the code want to cast a Stub to some of your type.





Hope that helps,

-- Brice

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/l787aZgnOaMJ.
Reply all
Reply to author
Forward
0 new messages