在 Spring Boot 2.x 中应用JUnit 4.x 进行单元测试

来自姬鸿昌的知识库
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>



BaseTest.java

package org.example;

import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class BaseTest {
}


在 IntelliJ IDEA 中快速建立单元测试类

保持要创建单元测试类的类文件的 tab 是当前选中的状态

生成缩略图出错:无法将缩略图保存到目标地点



菜单 “Navigate”> “Test”

生成缩略图出错:无法将缩略图保存到目标地点



选中使用的测试类库为 Junit4、指定父类、选中要测试的方法

生成缩略图出错:无法将缩略图保存到目标地点