Mockito Replace Method
Asked Answered
S

2

6

I have a Class:

    public class ProductComercialOrderDAO  extends BaseDao implements ProductComercialOrderDAOLocal  {

    private final static Logger log = UtilsBusiness.getLog4J(ProductComercialOrderDAO.class);

    public Session getSession() throws DAOServiceException{
        return super.getSession();
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void createProductComertialOrder(ProductComertialOrder pcom) throws DAOServiceException, DAOSQLException {

        log.debug("== Init createProductComertialOrder/ProductComercialOrderDAO ==");
        Session session = this.getSession();
        try {
            session.save(pcom);
            this.doFlush(session);
        } catch (Throwable ex) {
            log.error("== Exception in ProductComertialOrder ==");
            throw super.manageException(ex);
        } finally {
            log.debug("== createProductComertialOrder/ProductComercialOrderDAO End ==");
        }

    }
}

And I need to mock the method:

public Session getSession() throws DAOServiceException{
    return super.getSession();
}

for replace the Hibernate datasource in the jUnit test phase.

I have this code with Mockito:

package dao;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.util.Date;

import javax.ejb.EJB;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;

import co.com.myapp.sdii.exceptions.DAOSQLException;
import co.com.myapp.sdii.exceptions.DAOServiceException;
import co.com.myapp.sdii.model.pojo.ProductComercialOrder;
import co.com.myapp.sdii.persistence.dao.core.impl.ProductComercialOrderDAO;
import co.com.myapp.sdii.persistence.hibernate.HibernateUtil;

public class ProductComercialOrderDAOTest {

    public ProductComercialOrderDAO prodcomDao;

    private ProductComercialOrder prodCom;
    SessionFactory sessionFact = null;
    Session session = null;


    @Test
    public void setUp() throws DAOServiceException, DAOSQLException{
        sessionFact = util.HibernateUtil.getSessionFactory();
        sessionFact.openSession();
        session = sessionFact.getCurrentSession();

        prodCom = new ProductComercialOrder();
        prodCom.setCreationDate(new Date());
        prodCom.setCreationUser("user1");
        prodCom.setId(1L);
        prodCom.setPrdCategory("Hardware");
        prodCom.setPrdCategoryType("IRD");
        prodCom.setOrderNum(1L);

        if(prodcomDao == null){
            prodcomDao = new ProductComercialOrderDAO();
        }

        Mockito.spy(ProductComercialOrder.class);
        Mockito.when(prodcomDao.getSession()).thenReturn(session);
        prodcomDao.createProductComercialOrder(prodCom);        
    }


}

But when I call :

prodcomDao.createProductComercialOrder(prodCom);

The original ProductComercialOrderDAO.getSession() is called, instead of my mock.

How I can Mock the method for replace the hibernate session?

Saffier answered 24/10, 2016 at 21:5 Comment(0)
N
5

You need to do Mockito.doReturn(session).when(prodcomDao).getSession() to make sure the original method isn't called.

see @akcasoy's answer Mockito - difference between doReturn() and when()

Nosing answered 24/10, 2016 at 21:16 Comment(0)
G
0

I am seeing this question in 2024 and I think you have done the following mistake.

You are spying ProductComercialOrder instead of prodcomDao, (you want to replace getSession method of prodcomDao)

If it is not spied then I think the code line Mockito.when(prodcomDao.getSession()).thenReturn(session); itself will call the actual getSession method.

Once you correctly spy the required object/class, @supamanda's answer will be applicable.

Gasify answered 19/1 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.