MOCKITO 应用示例

package com.paic.wms.service.auditflow.impl;

 

import static org.junit.Assert.*;

 

import java.util.ArrayList;

import java.util.List;

 

import org.json.JSONArray;

import org.json.JSONObject;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

 

import com.paic.mms.dto.PsPaicEmpInfoDTO;

import com.paic.wp.dao.system.AuditFlowDAO;

import com.paic.wp.service.system.AuditFlowServiceImpl;

//Let's import Mockitostatically so that the code looks clearer

import static org.mockito.Mockito.*;

//import staticorg.powermock.api.mockito.PowerMockito.*; 

public class AuditFlowServiceImplTest {

    AuditFlowServiceImpl service;

    AuditFlowDAO daoMock;

    @Before

    public void setUp() throws Exception {

       service = new AuditFlowServiceImpl();

       daoMock = mock(AuditFlowDAO.class);

       service.setAuditFlowDAO(daoMock);

    }

    @Test

    public final void testSelectPsEmpInfoByUm() throws Exception {

       testUmIsNull();

       testUmIsNotNullAndPaPaicEmpInfoListIsNull();

       testUmIsNotNullAndPaPaicEmpInfoListIsNotNull();

    }

    @Test

    public final void testSelectPsEmpInfoByUmOrName() throws Exception {

       testUmIsNullForByUmOrName();

       testUmIsNotNullAndPaPaicEmpInfoListIsNullForByUmOrName();

       testUmIsNotNullAndPaPaicEmpInfoListIsNotNullForByUmOrName();

    }

    @After

    public void tearDown() throws Exception {

       service = null;

    }

   

    private void testUmIsNotNullAndPaPaicEmpInfoListIsNotNull() throws Exception{

       //given

       String um = "zhengxinliang001";

       List<PsPaicEmpInfoDTO> list = getPsPaicEmpInfoList();

       //when

       when(daoMock.selectPsEmpInfoByUm("zhengxinliang001")).thenReturn(list);

       PsPaicEmpInfoDTO dto = service.selectPsEmpInfoByUm(um);

       //then

       assertEquals("郑新良", dto.getLAST_NAME());

    }

 

    private List<PsPaicEmpInfoDTO> getPsPaicEmpInfoList() {

       PsPaicEmpInfoDTO dtoInServer = new PsPaicEmpInfoDTO();

      

       dtoInServer.setPAIC_UM_NUM("zhengxinliang001");

       dtoInServer.setLAST_NAME("郑新良");

       dtoInServer.setDEPTID("10000000");

       dtoInServer.setDEPT_NAME("平安机构");

      

       List<PsPaicEmpInfoDTO> list = newArrayList<PsPaicEmpInfoDTO>();

      

       list.add(dtoInServer);

       return list;

    }

 

    private void testUmIsNotNullAndPaPaicEmpInfoListIsNull() throws Exception {

       //given

       String um = "noBody001";

       //when

       when(daoMock.selectPsEmpInfoByUm("noBody001")).thenReturn(null);

       PsPaicEmpInfoDTO dto = service.selectPsEmpInfoByUm(um);

       //then

       assertEquals(dto, null);

    }

 

    private void testUmIsNull() throws Exception {

       //given

       String um = null;

       //when

       when(daoMock.selectPsEmpInfoByUm("")).thenReturn(null);

       PsPaicEmpInfoDTO dto = service.selectPsEmpInfoByUm(um);

       //then

       assertEquals(dto, null);

    }

   

    //um账号存在的时候

    private void testUmIsNotNullAndPaPaicEmpInfoListIsNotNullForByUmOrName()throws Exception{

       //given

       String um = "zhengxinliang001";

       //when

       when(daoMock.selectPsEmpInfoByUmOrName(um)).thenReturn(getPsPaicEmpInfoList());

       String umInfoFromService = service.selectPsEmpInfoByUmOrName(um);

       //then

       JSONObject umInfo = getPsEmpInfoJSON();

       assertEquals(umInfo.toString(), umInfoFromService);

    }

 

    private JSONObject getPsEmpInfoJSON() {

       JSONObject umInfo = new JSONObject();

       umInfo.put("um", "zhengxinliang001");

       umInfo.put("name", "郑新良");

       umInfo.put("deptNo", "10000000");

       umInfo.put("deptName", "平安机构");

      

       JSONArray empArray = new JSONArray();

       empArray.put(umInfo);

      

       JSONObject returnJSON = new JSONObject();

       returnJSON.put("size", 1);

       returnJSON.put("zhengxinliang001", empArray);

       return returnJSON;

    }

   

    //um账号不存在的时候

    private void testUmIsNotNullAndPaPaicEmpInfoListIsNullForByUmOrName()throws Exception{

       //given

       String um = null;

       //when

       when(daoMock.selectPsEmpInfoByUm("noBody001")).thenReturn(null);

       String umInfoFromService = service.selectPsEmpInfoByUmOrName(um);

       //then

       JSONObject umInfo = new JSONObject();

       umInfo.put("size", "0");

      

       assertEquals(umInfoFromService, umInfo.toString());

    }

   

    //um账号是空的时候

    private void testUmIsNullForByUmOrName() throws Exception {

       //given

       String um = null;

       //when

       when(daoMock.selectPsEmpInfoByUm("")).thenReturn(null);

       String umInfoFromService = service.selectPsEmpInfoByUmOrName(um);

       //then

       JSONObject umInfo = new JSONObject();

       umInfo.put("size", "0");

      

       assertEquals(umInfoFromService, umInfo.toString());

    }

 

}


原文地址:https://www.cnblogs.com/keanuyaoo/p/3304151.html