首页 > 代码库 > Mockito实例-Junit & Mockito

Mockito实例-Junit & Mockito

前言
    前几篇对Mockito的概念及其方法进行了介绍,本篇则提供了一个实例供大家练习,在我们工作中一般都是讲Junit与Mockito相结合一起使用,非常简便。
技术分享

正题
准备工作-引入Mockito与Junit

我们现在工作中的项目一般都是Maven项目,当然我们公司也不例外,所以前期准备就是先在pom.xml中引入这两个jar包。

<dependencies>
<dependency>
     <groupId>org.mockito</groupId>
     <artifactId>mockito-all</artifactId>
     <version>1.9.5</version>
</dependency>
<dependency>
     <artifactId>junit</artifactId>
     <groupId>junit</groupId>
     <version>4.11</version>  
</dependency>
</dependencies>


实例代码

package com.demandforce.services.setup;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

import com.demandforce.dao.BusinessDao;
import com.demandforce.dao.TextMessageTemplateDao;
import com.demandforce.model.Business;
import com.demandforce.model.TextMessageTemplate;

/**
 * 
 *   @author    Rebecca Zhao
 *   @date      Jan 6, 2014 
 *
 */
@RunWith(MockitoJUnitRunner.class)
public class MsgReminderServiceTest {
 @Mock
 TextMessageTemplateDao textMessageTemplateDao;
 
 @Mock
 BusinessDao businessDao;
 
 MsgReminderService msgReminderService;
 
 @Before
 public void setUpTest() {
  MockitoAnnotations.initMocks(this);
  msgReminderService = Mockito.spy(new MsgReminderServiceImpl());
  msgReminderService.setTextMessageTemplateDao(textMessageTemplateDao);
  msgReminderService.setBusinessDao(businessDao);
 }	
 
 @Test
 public void testCopyMsgReminderSetting(){
  Long parentBusId = 135001454L;
  String[] busIds = new String[]{"135001455","135001456"};
  Integer type = 204; 
  Mockito.doReturn(null).when(textMessageTemplateDao).selectByBusinessCategory(parentBusId, type);
  Mockito.doReturn(null).when(businessDao).selectById(parentBusId);
  List<Business> results = msgReminderService.copyMsgReminderSetting(parentBusId, busIds,type);
  assertTrue(results.isEmpty());	 

  com.demandforce.model.Business b = new com.demandforce.model.Business();
  b.setName("rebecca test");
  b.setCity("SFS");
  b.setState("C");
  TextMessageTemplate tmt = new TextMessageTemplate();
  tmt.setDelivery(1);
  tmt.setBusinessCalendar(true);
  tmt.setBusinessId(parentBusId);
  Mockito.doReturn(tmt).when(textMessageTemplateDao).selectByBusinessCategory(parentBusId, type);
  Mockito.doReturn(b).when(businessDao).selectById(135001455L);
  List<Business> res = msgReminderService.copyMsgReminderSetting(parentBusId, busIds,type);
  assertEquals(1,res.size());
 }
}


原类 

package com.demandforce.services.setup;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demandforce.dao.BusinessDao;
import com.demandforce.dao.TextMessageTemplateDao;
import com.demandforce.model.Business;
import com.demandforce.model.TextMessageTemplate;

/**
 * 
 *   @author    Rebecca Zhao
 *   @date      Jan 3, 2014 
 *
 */
@Service
public class MsgReminderServiceImpl implements MsgReminderService {
 static Logger logger = Logger.getLogger(TextMessageSettingServiceImpl.class);
 
 @Autowired
 TextMessageTemplateDao textMessageTemplateDao;
 
 @Autowired
 BusinessDao businessDao;
 
 @Override
 public List<Business> copyMsgReminderSetting(Long parentBusinessId,
   String[] businessIds,Integer type) {
  if(ArrayUtils.isEmpty(businessIds)){
       return null;
  }
  Integer delivery = null;
  Boolean businessCalendar = null;
  List<Business> list = new ArrayList<Business>();
     try{
      Long parentbusId = parentBusinessId;
      TextMessageTemplate tmt = textMessageTemplateDao.selectByBusinessCategory(parentbusId, type);
      if(tmt != null){
       delivery = tmt.getDelivery();
       businessCalendar = tmt.getBusinessCalendar();
      }
      if(delivery != null){
    for(String businessId : businessIds){
     Long busId = Long.valueOf(businessId);
     Business business = businessDao.selectById(busId);
     if(business != null) list.add(business);
    }
      }
     }catch(Exception e){
   logger.error("Error occur while copyTextMessageSetting", e);
  }
  return list;
 }

 public void setTextMessageTemplateDao(TextMessageTemplateDao textMessageTemplateDao) {
  this.textMessageTemplateDao = textMessageTemplateDao;
 }

 public void setBusinessDao(BusinessDao businessDao) {
  this.businessDao = businessDao;
 }
}


小结
上面实例就是简单展示了一个普通的Service的Test类如何编写,如果将其与Dao解耦,实现并向开发。

Mockito实例-Junit & Mockito