[英]Problem mocking hibernate's SessionFactory using Mockito
Any idea why the following mocking code does not work? 知道为什么下面的模拟代码不起作用?
org.hibernate.SessionFactory sessionFactory = Mockito.mock(SessionFactory.class);
org.hibernate.Session session = Mockito.mock(Session.class);
Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);
The thenReturn statement does not compile. thenReturn语句不编译。 "The method thenReturn(Session) in the type OngoingStubbing is not applicable for the arguments (Session)" But, why is it not applicable? “OngoingStubbing类型中的方法thenReturn(Session)不适用于参数(Session)”但是,为什么它不适用? I think I have the imports figured out correctly. 我想我的进口是正确的。
This is because the type actually returned by SessionFactory.getCurrentSession()
is org.hibernate.classic.Session
, which is a sub-type of org.hibernate.Session
. 这是因为通过实际返回的类型SessionFactory.getCurrentSession()
是org.hibernate.classic.Session
,这是一种子型的org.hibernate.Session
。 You'll need to change your mock to the correct type: 您需要将模拟更改为正确的类型:
org.hibernate.classic.Session session = Mockito.mock(org.hibernate.classic.Session.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.