当前位置: 首页> 房产> 政策 > SpringBoot整合Junit测试工程

SpringBoot整合Junit测试工程

时间:2025/7/15 11:31:55来源:https://blog.csdn.net/cplvfx/article/details/141109021 浏览次数:0次

SpringBoot整合Junit测试工程

需求

SpringBoot整合Junit
在这里插入图片描述

实现步骤

  1. 搭建SpringBoot工程
  2. 引入starter-test起步依赖
  3. 编写测试类
  4. 添加测试相关注解
    • @RunWith(SpringRunner.class)
    • @SpringBootTest(classes=启动类.class)
  5. 编写测试方法

1.搭建SpringBoot工程

在这里插入图片描述
Server URL:start.aliyun.com
项目名:jjycheng.spriongboot.junitDemo
语言:java
Tyoe:Maven
Group:jjycheng.SpringBootWebDemo
Artifact:spriongboot_junitDemo
KDJ:1.8
Java:17
Packging:Jar
然后,选择下一步
在这里插入图片描述
弹出下图窗口,直接点击Create,进行创建
在这里插入图片描述
完成后的项目截图

在这里插入图片描述

2. 引入starter-test起步依赖

生成的项目里你会发现他已经自动帮你引入了
我们打开pom.xml文件

在这里插入图片描述
代码如下:

<?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><groupId>jjycheng.SpringBootWebDemo</groupId><artifactId>spriongboot_junitDemo</artifactId><version>0.0.1-SNAPSHOT</version><name>jjycheng.spriongboot.junitDemo</name><description>jjycheng.spriongboot.junitDemo</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><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><version>${spring-boot.version}</version><configuration><mainClass>jjycheng.springbootwebdemo.spriongboot_junitdemo.Application</mainClass><skip>true</skip></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>

核心代码

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

这个spring-boot-starter-test就是测试需要引用的包

项目结构

在模块的src下有两个文件夹,分别是maintest;
main:是项目的核心文件夹
test:是项目的测试文件夹
在这里插入图片描述

重点

此时,你会发现maintest里面的文件结构是一致的。

3. 编写测试类

src》main》java》jjycheng 》springbootwebdemo》spriongboot_junitdemo》下新建UserService.java
在这里插入图片描述

main下的UserService.java内容

package jjycheng.springbootwebdemo.spriongboot_junitdemo;import org.springframework.stereotype.Service;@Service
public class UserService {public  void add(){System.out.println("My is UserService add()..........");}
}

4. 添加测试相关注解

- @RunWith(SpringRunner.class)
- @SpringBootTest(classes=启动类.class)

src》test》java》jjycheng 》springbootwebdemo》spriongboot_junitdemo》下新建UserServiceTest.java
在这里插入图片描述

test下的UserServiceTest.java内容

package jjycheng.springbootwebdemo.spriongboot_junitdemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;/*** UserService的测试类* **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
//在SpringBootTest里增加引导类文件,即mian文件夹下的启动文件
public class UserServiceTest {@Autowiredprivate  UserService userService;@Testpublic  void testAdd(){userService.add();}
}

5. 编写测试方法

test下的UserServiceTest.java内容

package jjycheng.springbootwebdemo.spriongboot_junitdemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;/*** UserService的测试类* **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
//在SpringBootTest里增加引导类文件,即mian文件夹下的启动文件
public class UserServiceTest {@Autowiredprivate  UserService userService;@Testpublic  void testAdd(){userService.add();}
}

在这里插入图片描述

注意以下注解

  • @RunWith(SpringRunner.class)
  • @SpringBootTest(classes = Application.class)
  • @Autowired
  • @Test
    在这里插入图片描述
    点击左侧运行按钮
    在这里插入图片描述
    控制台输出
    在这里插入图片描述
    在这里插入图片描述
关键字:SpringBoot整合Junit测试工程

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: