PowerMock / Mockito铸造模拟对象不起作用

时间:2015-01-20 09:26:41

标签: java hibernate junit mockito powermock

我在测试中初始化:

this.sessionMock = mock(Session.class);

在测试方法中,行:

((MySession)session).setRecordLimits(recordLimits)

给了我:

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

班级:

public class MySession extends AbstractSessionImpl implements EventSource

这是Mockito / Powermock的问题还是Hibernate的问题?对此有什么解决方法吗?

1 个答案:

答案 0 :(得分:3)

告诉Mockito / Powermock你要模仿Session,它无法知道你想要模拟的类实际上是MySession

由于您的代码实际上取决于MySession,您应该

this.sessionMock = mock(MySession.class);

代替