不要轻易放弃。学习成长的路上,我们长路漫漫,只因学无止境。 博主语录》》》

  您当前的位置是:网站首页 >博客信息

使用mock对service层进行单元测试

0
发布时间:『 2018-11-19 16:57』  博客类别: 单元测试   阅读(335)

    什么是mock?顾名思义mock就是模拟的意思,在service层的单元测试中引入mock概念可以切断与dao层的耦合,利用模拟出的mock来充当dao

    首先

    
   QQ鎴浘20181119164435.png

    @InjectMocks注解用来将带有@Mock注解的mapper注入到service

    @Mock用来声明一个mock

     @Before 单元测试前执行方法,初始化mock


    测试代码如下:


    @Test
    public void getMechanismTeacherList() throws Exception{
        String mechanismId="12";
        //教师信息结果集
        List<TeacherExtendsInfo> teacherExtendsInfoList=new ArrayList<TeacherExtendsInfo>();
        //教师信息实例
        TeacherExtendsInfo teacherExtendsInfo=new TeacherExtendsInfo();
        teacherExtendsInfo.setTeacherPic("http://39.105.6.180/download/f2018822104935955.png");
        teacherExtendsInfo.setTeacherEduBackground("2008届 浙江传媒学院本科,2010届 伯克利音乐学院研究生");
        teacherExtendsInfo.setTeacherHonor("国际级小提琴证书");
        teacherExtendsInfo.setTeacherId((long)57);
        teacherExtendsInfo.setTeacherMajor("小提琴,钢琴");
        teacherExtendsInfo.setTeacherName("缪心武");
        teacherExtendsInfo.setTeacherWorkhistory("2013-3 — 2012.5 美国哈萨斯克利特小提琴乐队领队,10年教学经验");
        teacherExtendsInfo.setTeacherQualifications("国家级老师与专家");
        
        //装填实例到
        teacherExtendsInfoList.add(teacherExtendsInfo);
        //Mock DAO层方法
        Mockito.when(customerinfoMapper.getMechanismTeacherList(mechanismId)).thenReturn(teacherExtendsInfoList);
        //教师信息DTO
        //模拟结果集
        List<TeacherInfoDTO> mechanismTeacherList=new ArrayList<TeacherInfoDTO>();        
        TeacherInfoDTO TeacherInfoDTO=new TeacherInfoDTO();        
        TeacherInfoDTO.setTeacherAvatar(teacherExtendsInfo.getTeacherPic());
        TeacherInfoDTO.setTeacherDesc(teacherExtendsInfo.getTeacherQualifications());
        List<String> teacherEduBackground=new ArrayList<String>();
        teacherEduBackground.add("2008届 浙江传媒学院本科");
        teacherEduBackground.add("2010届 伯克利音乐学院研究生");
        TeacherInfoDTO.setTeacherEducationBackground(teacherEduBackground);
        List<String> teacherHonor=new ArrayList<String>();
        teacherHonor.add("国际级小提琴证书");
        TeacherInfoDTO.setTeacherHonor(teacherHonor);
        TeacherInfoDTO.setTeacherId(teacherExtendsInfo.getTeacherId());
        TeacherInfoDTO.setTeacherMajor(teacherExtendsInfo.getTeacherMajor());
        TeacherInfoDTO.setTeacherName(teacherExtendsInfo.getTeacherName());
        List<String> teacherWorkExperience=new ArrayList<String>();
        teacherWorkExperience.add("2013-3 — 2012.5 美国哈萨斯克利特小提琴乐队领队");
        teacherWorkExperience.add("10年教学经验");
        TeacherInfoDTO.setTeacherWorkExperience(teacherWorkExperience);
        mechanismTeacherList.add(TeacherInfoDTO);
        
        TeacherListBean TeacherListBean=new TeacherListBean();
        TeacherListBean.setMechanismTeacherList(mechanismTeacherList);    
        TeacherListBean.setMechanismTeacherList(mechanismTeacherList);    
    
        //service测试
        Result<TeacherListBean>  result = nearbyServiceImplement.getMechanismTeacherList(mechanismId);
        Result<TeacherListBean>  result1 = nearbyServiceImplement.getMechanismTeacherList("1");
        
        //结果断言
        //有数据状态
        assertEquals(result.getData().getMechanismTeacherList().get(0).getTeacherAvatar(),Result.success(TeacherListBean).getData().getMechanismTeacherList().get(0).getTeacherAvatar());
        assertEquals(result.getData().getMechanismTeacherList().get(0).getTeacherDesc(),Result.success(TeacherListBean).getData().getMechanismTeacherList().get(0).getTeacherDesc());
        assertEquals(result.getData().getMechanismTeacherList().get(0).getTeacherEducationBackground(),Result.success(TeacherListBean).getData().getMechanismTeacherList().get(0).getTeacherEducationBackground());
        assertEquals(result.getData().getMechanismTeacherList().get(0).getTeacherHonor(),Result.success(TeacherListBean).getData().getMechanismTeacherList().get(0).getTeacherHonor());
        assertEquals(result.getData().getMechanismTeacherList().get(0).getTeacherId(),Result.success(TeacherListBean).getData().getMechanismTeacherList().get(0).getTeacherId());
        assertEquals(result.getData().getMechanismTeacherList().get(0).getTeacherMajor(),Result.success(TeacherListBean).getData().getMechanismTeacherList().get(0).getTeacherMajor());
        assertEquals(result.getData().getMechanismTeacherList().get(0).getTeacherName(),Result.success(TeacherListBean).getData().getMechanismTeacherList().get(0).getTeacherName());
        assertEquals(result.getData().getMechanismTeacherList().get(0).getTeacherTeachingAge(),Result.success(TeacherListBean).getData().getMechanismTeacherList().get(0).getTeacherTeachingAge());
        assertEquals(result.getData().getMechanismTeacherList().get(0).getTeacherWorkExperience(),Result.success(TeacherListBean).getData().getMechanismTeacherList().get(0).getTeacherWorkExperience());
        //无数据状态
        assertEquals(result1.getCode(), Result.noData().getCode());
        assertEquals(result1.getResult(), Result.noData().getResult());
        assertEquals(result1.getMsg(), Result.noData().getMsg());
        assertEquals(result1.getData(), Result.noData().getData());
    
    }


整个过程其实分为两点:

    一:mock一个预期结果

    二:调用service得到实际结果

    三:进行assert断言(用来比对mock值与实际值是否相等)

注意assert时要精确到字段级 本屌一开始assert mock对象和service结果对象 运行后一直报红,原因就是直接比对对象是比对对象的地址,对象的地址并不一定相等,assert只要value与预期相等就ok


关键字:     mock       单元测试