Java.lang.ClassCastException: Mockito

774 views
Skip to first unread message

purna pradeep

unread,
Dec 11, 2015, 5:44:47 AM12/11/15
to mockito
why jaxrs.Response  get() is throwing Class cast exception...

my test method 
 

   
 @Spy
     
MyDataAccess dataaccess;
     
@Mock
     
MyDataAccess dataaccess1;


   
MyGetResponse resp = new MyGetResponse();
   
    URI uri
= UriBuilder.fromUri(“myurlstring").build();
   
    when(dataaccess.buildWebTarget(“test"
, uri)).thenReturn(webTarget);
   
   
when(dataaccess.getResponse(webTarget)).thenReturn(resp);
   
//no classcast exception here
   
   
MyGetResponse actualResp = dataaccess1.retrievedata(“tst", “23”);
    // getting class cast exception here when getResponse invoked
    // internally in this method retrievedata



methods implementation in my Dataaccess class


 
  protected MyGetResponse retrievedata(String env,String Id){
       
        URI uri
= UriBuilder.fromUri(“myurlstring").build();
       
        WebTarget webTarget = buildWebTarget(“test"
, uri );
       
// web target is built in another method...
   
       
   
}


   
   
protected MyGetResponse getResponse(WebTarget webtarget){
   
       
Builder builder = webTarget.request();
       
return builder.accept(MediaType.APPLICATION_JSON)
                             
.accept(MediaType.APPLICATION_XML)
             
.get(MyGetResponse.class);
                             
// getting classcast exception here
       
}
       
   
}



not getting class cast exception when mocking but getting when actual method is executed..

Java.lang.ClassCastException: org.mockito.internal.creation.jmock.ClassImposterizer$ClassWithSuperclassToWorkAroundCglibBug$$EnhancerByMockitoWithCGLIB$$7159ca18 cannot be cast to MyGetResponse

purna pradeep

unread,
Dec 11, 2015, 5:44:48 AM12/11/15
to mockito

Brice Dutheil

unread,
Dec 12, 2015, 6:57:29 AM12/12/15
to moc...@googlegroups.com
Hi Pradeep,

We miss both the full stacktrace and too much code to understand what's wrong here. But it seems that some method is not mocked correctly as it returns a mock that doesn't have the right interfaces set.

-- Brice

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/2f679403-4cdd-41e0-a933-71a1e09fcf35%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages