SpringBoot整合Junit测试工程
需求
SpringBoot整合Junit
实现步骤
- 搭建SpringBoot工程
- 引入starter-test起步依赖
- 编写测试类
- 添加测试相关注解
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes=启动类.class)
- 编写测试方法
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
下有两个文件夹,分别是main
和test
;
main
:是项目的核心文件夹
test
:是项目的测试文件夹
重点
此时,你会发现main
和test
里面的文件结构是一致的。
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
点击左侧运行按钮
控制台输出