SpringBoot使用Mockito单元测试

单元测试的目的

单元测试的目的是用来确保程式的逻辑如你预期的方式执行,而并不是用来验证是否符合客户的需求的!通过单元测试来建立一道坚实的保障,确保代码在日后的修改中不会被破坏掉。在修改BUG阶段是无法完全做到谁产生的BUG就安排谁去修改因为对当前代码要满足的各种目的不熟悉,在修改一个模块或者BUG的时候把原有正确的功能也影响到了!更要命的是,谁也不知道这个BUG出现了,等待测试人员需要去重新发现一遍。然而如果这个项目在一开始就编写了单元测试的话,我们可以通过方便的自动化单元测试框架运行所有的单元测试,进而检查在此次修改前的所有被单元测试所覆盖的代码是否依然正常运行(符合以前编写的单元测试期望,如果验证通过,则认为原有代码未受到影响)。

单元测试常用框架

  • JUnit https://junit.org/
  • TestNG https://testng.org/
  • Hamcrest http://hamcrest.org/
  • EasyMock http://easymock.org/
  • Spock http://spockframework.org/
  • Mockito https://site.mockito.org/
  • PowerMock https://github.com/powermock/powermock/

UT的编写, 有一个非常重要的原则:拔!掉!网!线!也!能!跑!

Mockito分层mock

controller层

/**
 * UserContrlller的测试类 {@link UserController}
 */
@RunWith(MockitoJUnitRunner.class)
@SuppressWarnings("Duplicates")
public class UserControllerTest {

    @InjectMocks
    private UserController userController;

    @Mock
    private IUserService userService;

    @Test
    public void getUserList_success() throws Exception {

        // mock准备数据
        UserReq userReq = mockUserReq();
        PageInfo pageInfo = mockPageInfo();

        // 条件判断
        when(userService.getUserList(any(UserReq.class))).thenReturn(new BaseResponse<>(pageInfo));

        // 执行代码
        BaseResponse> baseResponse = userController.getUserList(userReq);

        // 断言
        assertNotNull(baseResponse.getData());
        assertThat(baseResponse.getData().getPageNum(), is(Integer.valueOf(userReq.getPageNum())));
        assertThat(baseResponse.getData().getPageSize(), is(Integer.valueOf(userReq.getPageSize())));
        assertThat(baseResponse.getData().getList().get(0).getLongminId(), is(userReq.getLongminId()));
        assertThat(baseResponse.getData().getList().get(0).getIsCertify(), is(Integer.valueOf(userReq.getIsCertify())));

        // 验证
        verify(userService, times(1)).getUserList(any(UserReq.class));
    }

    public UserReq mockUserReq() {
        UserReq userReq = new UserReq();
        userReq.setPageNum("1");
        userReq.setPageSize("10");
        userReq.setLongminId("zhangjianbing");
        userReq.setIsCertify("1");
        return userReq;
    }

    public PageInfo mockPageInfo() {
        PageInfo pageInfo = new PageInfo<>();
        User user = new User();
        user.setLongminId("zhangjianbing");
        user.setIsCertify(1);
        List list = new ArrayList<>();
        list.add(user);
        pageInfo.setList(list);
        pageInfo.setPageNum(1);
        pageInfo.setPageSize(10);
        return pageInfo;
    }

}

service层

/**
 * UserServiceImpl单元测
 */
@RunWith(MockitoJUnitRunner.class)
@SuppressWarnings("Duplicates")
public class UserServiceImplTest {

    @InjectMocks
    private UserServiceImpl userService;

    @Mock
    private UserMapper userMapper;

    @Test
    public void getUserList_success() {
        UserReq userReq = mockUserReq();
        List list = mockUserList();
        when(userMapper.selectList(any(QueryWrapper.class))).thenReturn(list);
        BaseResponse> baseResponse = userService.getUserList(userReq);
        assertTrue(baseResponse.getData().getList().size() != 0);
        // 再次模拟,需要跟源代码的方法一致
        verify(userMapper, Mockito.times(1)).selectList(any(QueryWrapper.class));
    }

    public UserReq mockUserReq() {
        UserReq userReq = new UserReq();
        userReq.setPageNum("1");
        userReq.setPageSize("10");
        userReq.setLongminId("zhangjianbing");
        userReq.setIsCertify("1");
        return userReq;
    }

    public List mockUserList() {
        List list = new ArrayList<>();
        User user1 = new User();
        User user2 = new User();
        User user3 = new User();
        Collections.addAll(list, user1, user2, user3);
        return list;
    }

}

dao层

先开启环境

/**
 * 开启测试的进程,模拟一个端口号
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WalletApplication.class)
@RunWith(SpringRunner.class)
public abstract class WalletApplicationTests {

}

dao的测试类继承启动类

/**
 * UserMapper的测试类 {@link UserMapper}
 */
@SuppressWarnings("Duplicates")
public class UserMapperTest extends WalletApplicationTests {

    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        UserReq userReq = mockUserReq();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(userReq.getIsCertify()), User::getIsCertify, userReq.getIsCertify())
                .eq(StringUtils.isNotBlank(userReq.getLongminId()), User::getLongminId, userReq.getLongminId());
        List userList = userMapper.selectList(queryWrapper);
        assertTrue(userList.size() != 0);
    }

    public UserReq mockUserReq() {
        UserReq userReq = new UserReq();
        userReq.setPageNum("1");
        userReq.setPageSize("10");
        userReq.setLongminId("");
        userReq.setIsCertify("");
        return userReq;
    }

}

依赖内存数据库H2,配置文件如下:

spring:
  datasource:
    driver-class-name: org.h2.Driver
    schema: classpath:db/schema-h2.sql
    data: classpath:db/data-h2.sql
    url: jdbc:h2:mem:test
    username: root
    password: test

data-h2.sql

DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
  (1, 'Jone', 18, '[email protected]'),
  (2, 'Jack', 20, '[email protected]'),
  (3, 'Tom', 28, '[email protected]'),
  (4, 'Sandy', 21, '[email protected]'),
  (5, 'Billie', 24, '[email protected]');

schema-h2.sql

DROP TABLE IF EXISTS user;
CREATE TABLE user
(
  id BIGINT(20) NOT NULL COMMENT '主键ID',
  name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
  age INT(11) NULL DEFAULT NULL COMMENT '年龄',
  email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
  PRIMARY KEY (id)
);

Mockito优缺点

  • Mockito无法模拟构造方法,静态方法,私有方法,后续会补上PowerMockito的基本使用。
  • Mockito简单容易上手,代码清晰明了,就是代码量有点多,Spock是个很棒的框架,它是Groovy语言写的,底层使用的是JUnit,研究一下,后续补上。

你可能感兴趣的:(SpringBoot使用Mockito单元测试)