Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

0
Classcastexception in casting with Powermockito? How to cast?
Post Flair (click to view more posts with a particular flair)
Post Body

Hi, just wanted to ask how do I cast an object to another to use for testing. This is my target class with a method called createByteData that has 3 parameters (IByteMessage, IByteMessage, IByteMessage). telegaram is an instance of IByteMessage.

byteData = controller.createByteData((IByteMessage) extensionHeader, (IByteMessage) header, telegaram);

this is my test class:

@Test

    public void test017() {
       when(logger.isTrace()).thenReturn(true);
       ITelegramExtensionHeader tempextheader = mock(ITelegramExtensionHeader.class);
       ITelegramHeader tempheader = mock(ITelegramHeader.class);
       IByteMessage tempmessage = mock(IByteMessage.class);
       AppDiscrimination tempapp = mock(AppDiscrimination.class);
       IByteMessage msg1 = mock(IByteMessage.class);
       IByteMessage msg2 = mock(IByteMessage.class);

       DataNotifyQueueData result = null;      

       byte[] i = {4, 5};

       when(tempmessage.toByteArray()).thenReturn(i);

       when((IByteMessage) tempextheader).thenReturn(msg1);
       when((IByteMessage) tempheader).thenReturn(msg2);

       result = m.createDataNotifyQueueDataInfo(tempextheader, tempheader, tempmessage, tempapp, "gramid", "this_is_the_value", true);
    }

the test class is a bit of a mess out of the trial and error I did, initially I didn't have the thenReturn(msg1) and thenReturn(msg2) I just added that hoping it might work. the error is below. I don't know but the target test class came with the code to cast ITelegramExtensionHeader and ITelegramHeader to IByteMessage so I am assuming it can be done. But when I run my test class it just gives this error. I don't know how to proceed from here. Any help? Thank you.

error:

java.lang.ClassCastException: jp.vic.vis.ps.telegram.inp.ITelegramExtensionHeader$MockitoMock$1374956466 cannot be cast to jp.co.mitsubishielectric.kouei.xftbase.bytemessage.IByteMessage
       at jp.vic.vis.ps.queue.AppQueueControllerTest.__CLR4_3_1v2uf1xesd(AppQueueControllerTest.java:363)
       at jp.vic.vis.ps.queue.AppQueueControllerTest.test017(AppQueueControllerTest.java:351)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)

Author
Account Strength
90%
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
2,724
Link Karma
1,904
Comment Karma
820
Profile updated: 1 day ago
Posts updated: 2 weeks ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago