前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >两分钟带你入门测试用例

两分钟带你入门测试用例

作者头像
千羽
发布2021-01-14 15:09:26
3520
发布2021-01-14 15:09:26
举报
文章被收录于专栏:程序员千羽程序员千羽

初识Mockito

Mockito是一个针对Java的mocking框架。将模拟对象来代替真实对象。

  1. 模拟任何外部依赖并将这些模拟对象插入测试代码中
  2. 执行测试中的代码
  3. 验证代码是否按照预期执行

前期工作

打开IDEA,new--> project --> Spring Initializr--> ..-->添加Spring Web...-->新建项目。

创建Student

代码语言:javascript
复制
package com.nateshao.domain;
/**
 * @Author TongJie Shao
 * Created by @Author  on 2020/7/11 23:46
 */

public class Student {
    private int id;
    private int age;
    private String name;

    public Student(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

创建StudentDao

代码语言:javascript
复制
package com.nateshao.dao;

import com.nateshao.domain.Student;
import org.springframework.stereotype.Component;

/**
 * @Author TongJie Shao
 * Created by @Author  on 2020/7/11 23:46
 */
@Component
public class StudentDao {
    public Student getStudentById(int id){
        return new Student(1,10,"nateshao");
    }
}

创建StudentService

代码语言:javascript
复制
package com.nateshao.service;

import com.nateshao.dao.StudentDao;
import com.nateshao.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author TongJie Shao
 * Created by @Author  on 2020/7/11 23:46
 */
@Service
public class StudentService {

    @Autowired
    private StudentDao studentDao;

    public Student getStudentById(int id){
        return studentDao.getStudentById(id);
    }
}

新建测试类StudentServiceTest

代码语言:javascript
复制
package com.nateshao.service;

import com.nateshao.dao.StudentDao;
import com.nateshao.domain.Student;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @Author TongJie Shao
 * Created by @Author  on 2020/7/11 23:51
 */
@SpringBootTest
class StudentServiceTest {

    @Autowired
    private StudentService studentService;
    @MockBean // 不走数据库
    StudentDao studentDao;

    @BeforeEach
    void setUp() {  // mockito一般与when搭配
        Mockito.when(studentDao.getStudentById(1)).thenReturn(new Student(1,10,"nateshao"));
    }

    @Test
    void getStudentById() {
        Student student = studentService.getStudentById(1);
        assertNotNull(student);
        assertEquals(student.getId(),1);
        assertEquals(student.getAge(),10);
        assertEquals(student.getName(),"nateshao");
    }
}

测试结果

测试结果

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 千羽的编程时光 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 初识Mockito
  • 前期工作
  • 创建Student
  • 创建StudentDao
  • 创建StudentService
  • 新建测试类StudentServiceTest
  • 测试结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档