SessionFactory Hibernation Problem Using Mockito

Any idea why the following mocking code is not working?

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 (Session) method in OngoingStubbing type is not applicable for arguments (session)" But why is this not applicable? I think the import is correctly clarified.

+4
source share
1 answer

This is because the type actually returned by SessionFactory.getCurrentSession() is org.hibernate.classic.Session , which is a subtype of org.hibernate.Session . You will need to change your layout to the correct type:

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

All Articles