简体   繁体   中英

PowerMock / Mockito casting mocked objects not working

I initialize in my test:

this.sessionMock = mock(Session.class);

And in the method under test the line:

((MySession)session).setRecordLimits(recordLimits)

gives me:

 java.lang.ClassCastException:
 org.hibernate.Session$$EnhancerByMockitoWithCGLIB$$8561a329 
 cannot be cast to myApp.MySession

The class:

public class MySession extends AbstractSessionImpl implements EventSource

Is it a problem of Mockito / Powermock or is it a Problem with Hibernate? And is there any fix for this?

By telling Mockito/Powermock that you want to mock Session , it has no way to know the class you want to mock is actually MySession .

Due to your code actually depends on a MySession , you should do

this.sessionMock = mock(MySession.class);

instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM