gpt4 book ai didi

java - Mockito 替换方法

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:14 26 4
gpt4 key购买 nike

我有一个类(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 ==");
}

}
}

我需要模拟这个方法:

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

用于在 jUnit 测试阶段替换 Hibernate 数据源。

我在 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);
}


}

但是当我打电话时:

prodcomDao.createProductComercialOrder(prodCom);

原始的 ProductCommercialOrderDAO.getSession() 被调用,而不是我的模拟。

如何模拟替换 hibernate session 的方法?

最佳答案

你需要做的Mockito.doReturn(session).when(prodcomDao).getSession()以确保未调用原始方法。

查看@akcasoy 的回答 Mockito - difference between doReturn() and when()

关于java - Mockito 替换方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40227554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com