使用mockmvc和mockito模拟spring + hibernate

时间:2016-06-22 10:47:39

标签: spring hibernate unit-testing mockito mockmvc

我在我的项目中使用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。有人可以帮忙解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

  1. 你必须一直嘲笑list()方法:
  2. 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,因为这是您正在测试的类。
    2. domainDaoImpl.setSessionFactory(hibernateSessionFactory);

      您没有这种方法,因此您必须添加它,并在此方法上使用@Autowired而不是在字段上:

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

答案 1 :(得分:0)

使用这些代码行

  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);