IDEA進行單元測試簡例

一、新建工程

 

二、新建類

Student

package com.cattle.mockito.domain;

/**
 * @author by XXX
 * @descriptaion #
 * @date 2020/6/11
 */
public class Student {

    private int id;

    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 Student(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    private int age;

    private String name;
}

StudentDao

package com.cattle.mockito.dao;

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

/**
 * @author by XXX
 * @descriptaion #
 * @date 2020/6/11
 */
@Component
public class StudentDao {

    public Student getStudentById(int id) {
        return new Student(1, 10, "Tony");
    }
}

StudentService

package com.cattle.mockito.service;

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

/**
 * @author by XXX
 * @descriptaion #
 * @date 2020/6/11
 */
@Service
public class StudentService {

    @Autowired
    StudentDao studentDao;

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

}

Pom

自動生成的pom有缺失需添加以下依賴

<?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.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cattle</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.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

生成測試類

package com.cattle.mockito.service;

import com.cattle.mockito.dao.StudentDao;
import com.cattle.mockito.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.util.Assert;


/**
 * @author by XXX
 * @descriptaion #
 * @date 2020/6/11
 */
@SpringBootTest
class StudentServiceTest {

    @Autowired
    StudentService studentService;

    @Autowired
    StudentDao studentDao;
    @BeforeEach
    void setUp() {
        // 執行測試之前進行更改
        Mockito.when(studentDao.getStudentById(1)).thenReturn(new Student(1, 18, "cattle"));
    }

    @Test
    void getStudentById() {
        Student student = studentService.getStudentById(1);
        System.out.println(student.getAge());
        System.out.println(student.getName());
        System.out.println(student.getId());
    }



}

源碼:https://gitee.com/jakhyd/JunitTest

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章