【问题标题】:Mocking spring + hibernate using mockmvc and mockito使用 mockmvc 和 mockito 模拟 spring + hibernate
【发布时间】:2016-06-22 10:47:38
【问题描述】:

我在我的项目中使用 spring + hibernate,下面是我的 java 代码

 @Repository
    @Transactional
    public class DomainDaoImpl implements DomainDao {

     static Logger logger = LoggerFactory.getLogger(DomainDaoImpl.class);

     @Autowired
     private SessionFactory hibernateSessionFactory;

     private static final String queryForDomains = "from Domain";
    @Override
    public List<Domain> getListOfALMDomains() throws CustomException {
    logger.info("Getting List of ALM domains");
    List<Domain> domains = null;
    try {
      Session session = null;
      domains = this.hibernateSessionFactory.getCurrentSession().createQuery(queryForDomains).list();
      logger.info("Exiting getListOfALMDomains method");
    } catch (Exception e) {
      logger.error("Exception occurred : " + e);
      throw new CustomException("Please contact your administrator. Unable to retrieve the domains.");
    }

    return domains;
      }
     }

下面是我的单元测试,我正在尝试模拟 hibernateSessionFactory。

@Test
@Transactional
public void getListOfDomainFromDomainImpl() throws Exception{
 String queryForDomains = "from Domain";
 Domain domainOne = new Domain();
 domainOne.setDomainId(4);
 domainOne.setDomainName("ADP");
 Domain domainSecond = new Domain();
 domainSecond.setDomainId(11);
 domainSecond.setDomainName("ABP");
 List<Domain> domains = new ArrayList<Domain>();
 domains.add(domainOne);
 domains.add(domainSecond);
 DomainDaoImpl domainDaoImpl = new DomainDaoImpl();
 domainDaoImpl = mock(DomainDaoImpl.class);
 when(domainDaoImpl.getListOfALMDomains()).thenReturn(domains);
 this.hibernateSessionFactory = mock(SessionFactory.class);
 when(hibernateSessionFactory.getCurrentSession().
 createQuery(queryForDomains) .list()).thenReturn(domains);
}
}

我在行收到 NullPointerException

 when(hibernateSessionFactory.getCurrentSession().
 createQuery(queryForDomains).list()).thenReturn(domains);

我没有办法模拟 hibernateSessionFactory。有人可以帮忙解决这个问题吗?

【问题讨论】:

    标签: spring hibernate unit-testing mockito mockmvc


    【解决方案1】:
    1. 您必须一直模拟到 list() 方法:

    Session session = mock(Session.class); Query query = mock(Query.class); when(hibernateSessionFactory.getCurrentSession()).thenReturn(session); when(session.createQuery(anyString())).thenReturn(query); when(query.list()).thenReturn(domains);

    1. 不要模拟 DomainDaoImpl,因为这是您正在测试的类。

    domainDaoImpl.setSessionFactory(hibernateSessionFactory);

    你没有这个方法,所以你必须添加它,并在这个方法而不是字段上使用@Autowired:

    @Autowired
    public void setSessionFactory(SessionFactory sf) {this.hibernateSessionFactory = sf;}
    
    1. 在测试中将模拟会话工厂设置为 DomainDaoImpl:

    domainDaoImpl.setSessionFactory(hibernateSessionFactory);

    【讨论】:

      【解决方案2】:

      使用这几行代码

        Session  session = mock(Session.class);
        SessionFactory  sessionFactory = mock(SessionFactory.class);
         Query query = mock(Query.class);
      when(sessionFactory.getCurrentSession()).thenReturn(session);    
      when(session.createQuery(queryForDomains)).thenReturn(query );
      when(query.list()).thenReturn(domains);
      

      【讨论】:

        猜你喜欢
        • 2016-02-05
        • 2014-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多