Jeudy, Guillaume | 6 May 21:11 2009

ClassCastException with generic method

Hi,
 
I'm trying to test a generic method. Is that possible with jmock2? I tried  one(jaxbUnmarshaller).unmarshal(with(any(Class.class), with(any(String.class))) but that also fails.
 
expectations code:

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

interface:
 
public interface JAXBUnmarshaller {
 <T> T unmarshal(Class<T> expectedType, InputStream inputStream) 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 | 6 May 21:57 2009
Picon

Re: ClassCastException with generic method

Before we go any further, you're expecting a string but the interface  
says InputStream.

Is that correct?

S.

On 6 May 2009, at 20:11, Jeudy, Guillaume wrote:

> Hi,
>
> I'm trying to test a generic method. Is that possible with jmock2? I  
> tried  one(jaxbUnmarshaller).unmarshal(with(any(Class.class),  
> with(any(String.class))) but that also fails.
>
> expectations code:
>
> this.mockContext.checking(new Expectations() {{
>   one(jaxbUnmarshaller).unmarshal(with(Issue.class),  
> with(any(String.class)));
>  }});
>
> interface:
>
> public interface JAXBUnmarshaller {
> <T> T unmarshal(Class<T> expectedType, InputStream inputStream)  
> 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

Jeudy, Guillaume | 6 May 22:06 2009

RE: ClassCastException with generic method

Sorry I trimmed the wrong code from the interface on the previous email. Here's the correct interface:
 
public interface JAXBUnmarshaller {
 <T> T unmarshal(Class<T> expectedType, String text) throws JAXBException;
}
 
Guillaume
From: Steve Freeman [mailto:steve-/6t1Ty3A97Vaa/9Udqfwiw@public.gmane.org]
Sent: Wed 5/6/2009 3:57 PM
To: user-sXN/XchZ9OexIXFVlbCvtR2eb7JE58TQ@public.gmane.org
Subject: Re: [jmock-user] ClassCastException with generic method

Before we go any further, you're expecting a string but the interface 
says InputStream.

Is that correct?

S.


On 6 May 2009, at 20:11, Jeudy, Guillaume wrote:

> Hi,
>
> I'm trying to test a generic method. Is that possible with jmock2? I 
> tried  one(jaxbUnmarshaller).unmarshal(with(any(Class.class), 
> with(any(String.class))) but that also fails.
>
> expectations code:
>
> this.mockContext.checking(new Expectations() {{
>   one(jaxbUnmarshaller).unmarshal(with(Issue.class), 
> with(any(String.class)));
>  }});
>
> interface:
>
> public interface JAXBUnmarshaller {
> <T> T unmarshal(Class<T> expectedType, InputStream inputStream) 
> 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




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.
Work | 11 May 09:26 2009
Picon

Imposteriser produces ClassCastException when JUnit tests are run with Ant.

Hi All, just wasted a day on this.

I am mocking Concrete classes using the standard Imposteriser.INSTANCE.

The tests run fine under the Eclipse IDE.

However when the same tests are run using the Ant JUnit task they fail with a ClassCastException.

The condition required to reproduce this bug is to mock a concrete class (or its subclass) more than
once over several test classes. Typically the first use of the Mock class succeeds, but subsequent
Test classes using the same mocked concrete class fail. Commenting out the first usage
will allow the second usage (which failed) to succeed.

Debugging into the code seems to show CGLIBed instances of the expected class.

The failure is in:

    public <T> T imposterise(final Invokable mockObject, Class<T> mockedType, Class<?>... ancilliaryTypes) {        if (!mockedType.isInterface() && toStringMethodIsFinal(mockedType)) {            throw new IllegalArgumentException(mockedType.getName() + " has a final toString method");        }                try {            setConstructorsAccessible(mockedType, true);            Class<?> proxyClass = createProxyClass(mockedType, ancilliaryTypes);            return mockedType.cast(createProxy(proxyClass, mockObject));        }        finally {            setConstructorsAccessible(mockedType, false);        }    }


On this line:

            return mockedType.cast(createProxy(proxyClass, mockObject));
I have converted my own classes to use interfaces and removed imposteriser (using just interfaces with the imposteriser also fails).
However I am now stuck because I am also mocking 3rd part classes.
Any workarounds appreciated.

P.S. I have upgraded from 2.5.1 to 2.6.o without improvement.
Steve Freeman | 11 May 10:39 2009
Picon

Re: Imposteriser produces ClassCastException when JUnit tests are run with Ant.

Sorry to be a pain, would you mind filing an issue for this.

Thanks

S

On 11 May 2009, at 08:26, Work wrote:

> Hi All, just wasted a day on this.
>
> I am mocking Concrete classes using the standard  
> Imposteriser.INSTANCE.
>
> The tests run fine under the Eclipse IDE.
>
> However when the same tests are run using the Ant JUnit task they  
> fail with
> a ClassCastException.
>
> The condition required to reproduce this bug is to mock a concrete  
> class (or
> its subclass) more than
> once over several test classes. Typically the first use of the Mock  
> class
> succeeds, but subsequent
> Test classes using the same mocked concrete class fail. Commenting  
> out the
> first usage
> will allow the second usage (which failed) to succeed.
>
> Debugging into the code seems to show CGLIBed instances of the  
> expected
> class.
>
> The failure is in:
>
>    public <T> T imposterise(final Invokable mockObject, Class<T>
> mockedType, Class<?>... ancilliaryTypes) {        if
> (!mockedType.isInterface() && toStringMethodIsFinal(mockedType)) {
> throw new IllegalArgumentException(mockedType.getName() + " has a  
> final
> toString method");        }                try {
> setConstructorsAccessible(mockedType, true);            Class<?>  
> proxyClass
> = createProxyClass(mockedType, ancilliaryTypes);            return
> mockedType.cast(createProxy(proxyClass, mockObject));        }
> finally {            setConstructorsAccessible(mockedType,  
> false);        }
> }
>
>
> On this line:
>
>            return mockedType.cast(createProxy(proxyClass,  
> mockObject));
> I have converted my own classes to use interfaces and removed  
> imposteriser
> (using just interfaces with the imposteriser also fails).
> However I am now stuck because I am also mocking 3rd part classes.
> Any workarounds appreciated.
>
> P.S. I have upgraded from 2.5.1 to 2.6.o without improvement.
>

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

Wim Deblauwe | 11 May 10:42 2009
Picon

Re: Changing expectations


Hi,

is there already documentation on this?

regards,

Wim

Dale King-2 wrote:
> 
> What he probably doesn't realize (and this seems to be a big stumbling
> block
> for new users) is that you can break your expectations up into multiple
> context.checking calls. I know there have been several people come here
> with
> the same issue.
> 
> The cheat sheet documentation really needs to be updated to make this
> clear.
> I submitted a JIRA issue for it.
> 
> On Nov 26, 2007 2:59 PM, Nat Pryce <nat.pryce@...> wrote:
> 
>> On 26/11/2007, Harald Ueland <harald@...> wrote:
>> > Is there a way to change return values for expectations after they are
>> set?
>>
>> No.
>>
>> > I want to do this for some tests where 99 % of the test methods use
>> predefined
>> > expectations set up from setUp(), but a few needs other return values.
>> >
>> > How can this be done without calling a setup method in all the tests?
>>
>> It cannot.  That's because pulling the common setup into a clearly
>> named utility method makes tests *much* easier to understand.
>>
>> --Nat
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
> 
> 
> -- 
> Dale King
> 
> 

--

-- 
View this message in context: http://www.nabble.com/Changing-expectations-tp13949061p23479296.html
Sent from the jMock - User mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email

Jeudy, Guillaume | 11 May 15:41 2009

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.
Ignat Zapolsky | 11 May 15:47 2009
Picon

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.
Steve Freeman | 11 May 15:58 2009
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

Jeudy, Guillaume | 11 May 15:59 2009

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.

Gmane