“在 Spring Boot 2.x 中应用JUnit 4.x 进行单元测试”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“1”的新页面)
 
 
(未显示同一用户的5个中间版本)
第1行: 第1行:
1
+
=== pom.xml ===
 +
<syntaxhighlight lang="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>
 +
</syntaxhighlight>
 +
 
 +
 
 +
 
 +
 
 +
=== BaseTest.java ===
 +
<syntaxhighlight lang="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 {
 +
}
 +
</syntaxhighlight>
 +
 
 +
 
 +
=== 在 IntelliJ IDEA 中快速建立单元测试类 ===
 +
 
 +
==== 保持要创建单元测试类的类文件的 tab 是当前选中的状态 ====
 +
[[文件:指定为哪个类创建单元测试类.png|无|缩略图|530x530像素]]
 +
 
 +
 
 +
 
 +
 
 +
==== 菜单 “Navigate”> “Test” ====
 +
[[文件:Navigate Test.png|无|缩略图|502x502像素]]
 +
 
 +
 
 +
 
 +
 
 +
==== 选中使用的测试类库为 Junit4、指定父类、选中要测试的方法 ====
 +
[[文件:进行单元测试类的相关配置.png|无|缩略图|495x495像素]]

2023年1月28日 (六) 22:54的最新版本

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 是当前选中的状态

指定为哪个类创建单元测试类.png



菜单 “Navigate”> “Test”

Navigate Test.png



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

进行单元测试类的相关配置.png