Jeudy, Guillaume | 11 May 2009 15:41
Favicon

ClassCastException with generic method

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.
Steve Freeman | 11 May 2009 16:00
Picon

Re: ClassCastException with generic method

Ah, just seen it.

your first argument is a /class/, not an instance.

Can you try:

one(jaxbUnmarshaller).unmarshal(with(equalTo(Issue.class)),  
with(any(String.class)));

Or something like that?

S.

On 11 May 2009, at 14:41, Jeudy, Guillaume wrote:
> 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;
> }

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Steve Freeman | 11 May 2009 15:58
Picon

Re: ClassCastException with generic method

Should be. Can you trap the onMessage() call (in a debugger or  
printout) and make sure of the actual types of object being passed in?

Ta
S.

On 11 May 2009, at 14:41, Jeudy, Guillaume wrote:
> 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.

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Ignat Zapolsky | 11 May 2009 15:47
Picon
Gravatar

Re: 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.
Jeudy, Guillaume | 11 May 2009 15:59
Favicon

RE: ClassCastException with generic method

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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org]
Sent: Mon 5/11/2009 9:47 AM
To: user-sXN/XchZ9OexIXFVlbCvtR2eb7JE58TQ@public.gmane.org
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.
Steve Freeman | 11 May 2009 16:28
Picon

Re: ClassCastException with generic method

I think because Java erases the type by the time it comes to us. In  
this case you could write a custom action that took the class passed  
into the call and uses it to create an instance. Not sure it's worth  
the trouble, though.

S.

On 11 May 2009, at 14:59, Jeudy, Guillaume wrote:
> 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.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane