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.
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)
Subreddit
Post Details
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnprogra...