springBoot中使用Junit和Mockito进行单元测试

概述

比如现在有一个场景。需要测试一个方法,但是该方法里面有一个分支方法,这个方法需要调用外部接口方法,现在接口还未实现,我们实际测试的时候想跳过这个方法继续往后执行。那么我们就可以通过Mockito对外部服务进行模拟、打桩,从而达到对测试方法的完整测试。

过程

需要注意的语句:
Mockito.when(A).thenReturn(B);

需要注意的注解:
@InjectMocks
说明:该注解跟@Autowired类似,注入一个服务。
@Mock
说明:注入一个服务,该服务会是“假对象”,并不会真正执行。

ApplicationTest.java

package com.shushan;

import com.shushan.dao.UserHandleDao;
import com.shushan.entity.Student;
import com.shushan.service.impl.UserHandleServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {

    @InjectMocks
    private UserHandleServiceImpl userHandleServiceImpl;

    @Mock
    private UserHandleDao userHandleDao;

   /**
     * 定义需要“假执行”的方法,并预先设定好该方法需要返回的值。当底层执行到该方法时,
     * 不会真正执行方法并返回自己预先设定好的值。
     * 注意点:实际调用方法传入的参数必须跟预先设定“假执行”的方法参数一致,否则不生效。
     * 如下:预先定义“假执行”userHandleDao.findUserListByCode("1001")的方法参数是1001,
     * 那么userHandleServiceImpl.findUserListByCode("1001")传入的但是也必须为1001
     */
    @Test
    public void myTest1() {
        List list = new ArrayList();
        Student stu = new Student();
        stu.setId(111);
        stu.setName("测试姓名");
        list.add(stu);
        Mockito.when(userHandleDao.findUserListByCode("1001")).thenReturn(list);
        List<Student> relist = userHandleServiceImpl.findUserListByCode("1001");
        System.out.println(relist);
    }

    
    /**
     * 另一种情况:service层跟dao层方法参数实在做不到一致。
     *  如下:service层userHandleServiceImpl.findUserByParam("1004", "李四");
     *        dao层userHandleDao.findUserAll(Student);
     *   这种情况我们使用Mockito.isA(Student.class); 定义只有方法参数
     *   是类对象Student就行
     */
    @Test
    public void myTest2() {
        List list = new ArrayList();
        Student stu2 = new Student();
        stu2.setName("李四");
        list.add(stu2);
        Mockito.when(userHandleDao.findUserAll(Mockito.isA(Student.class))).thenReturn(list);
        List<Student> relist = userHandleServiceImpl.findUserByParam("1004", "李四");
    }

}

UserHandleService.java

package com.shushan.service;

import com.shushan.entity.Student;

import java.util.List;

public interface UserHandleService {

    public List<Student> findUserListByCode(String code);

    public List<Student> findUserByParam(String code, String name);

}

UserHandleServiceImpl.java

package com.shushan.service.impl;

import com.shushan.dao.UserHandleDao;
import com.shushan.entity.Student;
import com.shushan.service.UserHandleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserHandleServiceImpl implements UserHandleService {

    @Resource
    private UserHandleDao userHandleDao;

    /**
     * 通过学号查询学生信息
     * @param code
     * @return
     */
    @Override
    public List<Student> findUserListByCode(String code) {
        return userHandleDao.findUserListByCode(code);
    }

   /**
     * 通过参数查询学生信息
     * @param code
     * @param name
     * @return
     */
    @Override
    public List<Student> findUserByParam(String code, String name) {
        Student student = new Student();
        student.setName("hahh");
        student.setCode(code);
        return userHandleDao.findUserAll(student);
    }

}

UserHandleDao.java

package com.shushan.dao;

import com.shushan.entity.Student;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserHandleDao {

    public List<Student> findUserListByCode(String code);

    public List<Student> findUserAll(Student student);
}

  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值