Java 模仿hibernate';使用Mockito的SessionFactory

Java 模仿hibernate';使用Mockito的SessionFactory,java,hibernate,mocking,mockito,Java,Hibernate,Mocking,Mockito,你知道为什么下面的模拟代码不起作用吗 org.hibernate.SessionFactory sessionFactory = Mockito.mock(SessionFactory.class); org.hibernate.Session session = Mockito.mock(Session.class); Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session); thenReturn语句不编译。

你知道为什么下面的模拟代码不起作用吗

org.hibernate.SessionFactory sessionFactory = Mockito.mock(SessionFactory.class);
org.hibernate.Session session = Mockito.mock(Session.class);
Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);
thenReturn语句不编译。 “OnGoingStubing类型中的thenReturn(会话)方法不适用于参数(会话)”
但为何不适用呢?我想我已经正确地计算了导入。

这是因为
SessionFactory.getCurrentSession()
实际返回的类型是
org.hibernate.classic.Session
,它是
org.hibernate.Session
的子类型。您需要将模拟更改为正确的类型:

org.hibernate.classic.Session session = Mockito.mock(org.hibernate.classic.Session.class);