简体   繁体   English

PowerMock / Mockito投射模拟对象不起作用

[英]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? 是Mockito / Powermock的问题还是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 . 通过告诉Mockito / Powermock您要模拟Session ,它无法知道您要模拟的类实际上是MySession

Due to your code actually depends on a MySession , you should do 由于您的代码实际上取决于MySession ,您应该这样做

this.sessionMock = mock(MySession.class);

instead 代替

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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