Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock SessionFactory or Session by PowerMockito in a project using Spring and Hibernate?

I am working on a project that uses Spring and Hibernate. I have an abstract DAO that contains SessionFactory. I need to mock SessionFactory or Session for my unit test. But I have not been successful so far. Below is the code. Anyone has any idea? Thank you.

Here is my AbstractDAO

public abstract class AbstractDAO {
    protected JdbcTemplate jdbcTemplate;
    protected SessionFactory sessionFactory;
    protected NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    ...

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
    }}

And here is my ConcreteDAO

public class LoginDAO extends AbstractDAO implements InitializingBean {
    ...
    public User getLoggedinUserByUserid(Long userid){
            log.info("in getLoggedinUserByUserid"); 
            User result = null;
            Session session = sessionFactory.openSession();
            try {
            session.beginTransaction();
            result = (User) session.get(User.class, userid);
            session.getTransaction().rollback();
            session.close();
            } catch (Exception e) {
            log.error(e,e);
            session.getTransaction().rollback();
            session.close();
            }

    return result;
     }
...}

and here is my test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "LoginDAOTest-context.xml" })
@PrepareForTest({SessionFactory.class, Session.class, Transaction.class})
public class LoginDAOTest2 extends BaseDAO {
    private static final Logger log = Logger.getLogger(LoginDAOTest2.class);
    @Rule
public PowerMockRule rule = new PowerMockRule();
private LoginDAO loginDAO = new LoginDAO();

private SessionFactory mockedSessionFactory;

private Session mockedSession;

private Transaction mockedTransaction;
@Autowired
        public void setSessionFactory(SessionFactory sessionFactoryCore) {
                mockedSessionFactory =  PowerMockito.mock( sessionFactoryCore.getClass());
                mockedSession = PowerMockito.mock(Session.class);
                PowerMockito.doReturn(mockedSession).when(mockedSessionFactory).openSession();
                PowerMockito.doReturn(mockedTransaction).when(mockedSession).beginTransaction();
  loginDAO.setSessionFactory(this.mockedSessionFactory);
        }
@Test
     public void shouldRollbackInGetLoggedinUserByUseridWhenSessionThrowsException() {
              // Given
            PowerMockito.doThrow(new Exception()).when(mockedSession).get(User.class, 12L);
            // When
            loginDAO.getLoggedinUserByUserid(12L);
            // Then
            verify(mockedTransaction, times(1)).rollback();
    }
}

and here is the result of junit.


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.aeon.ps.dao.LoginDAOTest2': Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/mockito/internal/MockitoInvocationHandler
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NoClassDefFoundError: org/mockito/internal/MockitoInvocationHandler
    at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:138)
    at com.aeon.ps.dao.LoginDAOTest2.setSessionFactory(LoginDAOTest2.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 26 more
Caused by: java.lang.ClassNotFoundException: org.mockito.internal.MockitoInvocationHandler
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 35 more

like image 611
sura watthana Avatar asked Dec 20 '22 17:12

sura watthana


1 Answers

Why don't do this :

@Before
public void setUp() {
    mockedSessionFactory =  Mockito.mock(SessionFactory.class);
    mockedSession = Mockito.mock(Session.class);
    mockedTransaction = Mockito.mock(Transaction.class);
    Mockito.when(mockedSessionFactory.openSession()).thenReturn(mockedSession);
    Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
    loginDAO.setSessionFactory(this.mockedSessionFactory);
}

instead of :

@Autowired
public void setSessionFactory(SessionFactory sessionFactoryCore) {
    mockedSessionFactory =  PowerMockito.mock( sessionFactoryCore.getClass());
    mockedSession = PowerMockito.mock(Session.class);
    PowerMockito.doReturn(mockedSession).when(mockedSessionFactory).openSession();
    PowerMockito.doReturn(mockedTransaction).when(mockedSession).beginTransaction();
    loginDAO.setSessionFactory(this.mockedSessionFactory);
}

In addition, it allows you to not use powermock.

like image 158
gontard Avatar answered Mar 01 '23 23:03

gontard