mockito简单案例使用

mockito简单案例使用

mockito主要用来模拟调用和返回结果,常被用在测试例中

1.项目结构

在studentDao中进行数据库查询,返回特定的数据,在单元测试中使用mock对StudentDao的返回结果进行模拟,返回想要的数据(与StudentDao中的实际返回结果不同),并进行断言判断
在这里插入图片描述

2.依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>mockito</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mockito</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.domain类

package com.example.mockito.domain;

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


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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public int getId() {
        return id;
    }

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

4.dao

package com.example.mockito.dao;

import com.example.mockito.domain.Student;
import org.springframework.stereotype.Component;

@Component
public class StudentDao {

    public Student getById(Integer id) {

        return new Student("张三", 20, 5);
    }
}

5.service

package com.example.mockito.service;

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

@Service
public class StudentService {
    @Autowired
    StudentDao studentDao;

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

6.主启动类

package com.example.mockito;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MockitoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MockitoApplication.class, args);
    }

}

7.测试类

创建测试类的快捷方法
选中要创建测试的类
ctrl+shift+t 或者commond+shift+t

package com.example.mockito.service;

import com.example.mockito.dao.StudentDao;
import com.example.mockito.domain.Student;


import org.junit.Assert;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;

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;


@SpringBootTest
class StudentServiceTest {

    @Autowired
    StudentService studentService;

    @MockBean
    StudentDao studentDao;


    @BeforeEach
    void setUp() {
        Mockito.when(studentDao.getById(5)).thenReturn(new Student("李四", 30, 5));
    }

    @Test
    void getStudentById() {
        Student student = studentService.getStudentById(5);

        Assert.assertNotNull(student);
        Assert.assertEquals(student.getAge(), 30);
        Assert.assertEquals(student.getName(), "李四");
    }
}

8.主测试类

无明显意义

package com.example.mockito;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class MockitoApplicationTests {

    @Test
    void contextLoads() {
    }

}

9.测试结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Guesshat/article/details/115045909