« Return to Thread: ClassCastException with generic method

RE: ClassCastException with generic method

by Guillaume Jeudy :: Rate this Message:

Reply to Author | View in Thread

Hi Ignat,
 
Your proposal fixes my problem! Thanks alot! 
 
 I'm not sure why in this case the return type can't be autodetected by JMock. In many other cases where the return value didn't matter I just omitted the will(returnValue()) expectation and it all worked fine.
 
Regards,
-Guillaume


From: Ignat Zapolsky [mailto:izapolsky@...]
Sent: Mon 5/11/2009 9:47 AM
To: user@...
Subject: Re: [jmock-user] ClassCastException with generic method

Hi Jeudy,,

Have you tried to return object of expected type, e.g. instanceof Issue as result of one(jaxbUnmarshaller).unmarshal(with(Issue.class), with(any(String.class))); expectation ?
Example:

one(jaxbUnmarshaller).unmarshal(with(Issue.class), with(any(String.class)));
will(returnValue(new Issue()));


I recon that correct return type may not be detected by JMock automatically and it will just return new Object instead of new Issue.

Jeudy, Guillaume wrote:
Hi,
 
I'm trying to test a generic method. Is that possible with jmock2?
expectations code:

 this.mockContext.checking(new Expectations() {{
   one(jaxbUnmarshaller).unmarshal(with(Issue.class), with(any(String.class)));
  }});

I tried:  one(jaxbUnmarshaller).unmarshal(with(any(Class.class), with(any(String.class))) but that also fails.

interface:
 
public interface JAXBUnmarshaller {
 <T> T unmarshal(Class<T> expectedType, String text) throws JAXBException;
}

throws:
 
java.lang.ClassCastException: java.lang.Object
 at a.b.msdmi.mdb.AJSubscriber.onMessage(AJSubscriber.java:65)
 at a.b.msdmi.mdb.AJSubscriberTest.testOnMessage_Success(AJSubscriberTest.java:41)
 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:585)
 at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
 at org.testng.internal.Invoker.invokeMethod(Invoker.java:478)
 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:617)
 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:885)
 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
 at org.testng.TestRunner.runWorkers(TestRunner.java:712)
 at org.testng.TestRunner.privateRun(TestRunner.java:582)
 at org.testng.TestRunner.run(TestRunner.java:477)
 at org.testng.SuiteRunner.runTest(SuiteRunner.java:324)
 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:319)
 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)
 at org.testng.SuiteRunner.run(SuiteRunner.java:198)
 at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:823)
 at org.testng.TestNG.runSuitesLocally(TestNG.java:790)
 at org.testng.TestNG.run(TestNG.java:708)
 at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)
Thanks,
-Guillaume


This electronic mail (including any attachments) may contain information that is privileged, confidential, and/or otherwise protected from disclosure to anyone other than its intended recipient(s). Any dissemination or use of this electronic email or its contents (including any attachments) by persons other than the intended recipient(s) is strictly prohibited. If you have received this message in error, please notify us immediately by reply email so that we may correct our internal records. Please then delete the original message (including any attachments) in its entirety. Thank you.



This electronic mail (including any attachments) may contain information that is privileged, confidential, and/or otherwise protected from disclosure to anyone other than its intended recipient(s). Any dissemination or use of this electronic email or its contents (including any attachments) by persons other than the intended recipient(s) is strictly prohibited. If you have received this message in error, please notify us immediately by reply email so that we may correct our internal records. Please then delete the original message (including any attachments) in its entirety. Thank you.

 « Return to Thread: ClassCastException with generic method