HM-SpringBoot1.4【SpringBoot整合Junit】

 

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.5.3</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.haifei</groupId>
12     <artifactId>springboot5-test</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>springboot5-test</name>
15     <description>Demo project for Spring Boot</description>
16     <properties>
17         <java.version>1.8</java.version>
18     </properties>
19     <dependencies>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-web</artifactId>
23         </dependency>
24 
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-test</artifactId>
28             <scope>test</scope>
29         </dependency>
30         <dependency>
31             <groupId>junit</groupId>
32             <artifactId>junit</artifactId>
33             <scope>test</scope>
34         </dependency>
35     </dependencies>
36 
37     <build>
38         <plugins>
39             <plugin>
40                 <groupId>org.springframework.boot</groupId>
41                 <artifactId>spring-boot-maven-plugin</artifactId>
42             </plugin>
43         </plugins>
44     </build>
45 
46 </project>
复制代码

 

复制代码
 1 package com.haifei.springboot5test;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class Springboot5TestApplication {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(Springboot5TestApplication.class, args);
11     }
12 
13 }
复制代码

 

复制代码
 1 package com.haifei.springboot5test;
 2 
 3 import org.junit.jupiter.api.Test;
 4 import org.springframework.boot.test.context.SpringBootTest;
 5 
 6 @SpringBootTest
 7 class Springboot5TestApplicationTests {
 8 
 9     @Test
10     void contextLoads() {
11     }
12 
13 }
复制代码

 

1

 

 

复制代码
 1 package com.haifei.springboot5test;
 2 
 3 import org.springframework.stereotype.Service;
 4 
 5 @Service
 6 public class UserService {
 7 
 8     public void add(){
 9         System.out.println("add...");
10     }
11 
12 }
复制代码

 

复制代码
 1 package com.haifei.test;
 2 
 3 import com.haifei.springboot5test.Springboot5TestApplication;
 4 import com.haifei.springboot5test.UserService;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.boot.test.context.SpringBootTest;
 9 import org.springframework.test.context.junit4.SpringRunner;
10 
11 /**
12  * UserService的测试类
13  */
14 @RunWith(SpringRunner.class)
15 @SpringBootTest(classes = Springboot5TestApplication.class)
16 public class UserServiceTest {
17 
18     @Autowired //注入待测试的对象
19     private UserService userService;
20 
21     @Test
22     public void testAdd(){
23         userService.add();
24     }
25 
26 }
复制代码

 

 

 

 

 2

当测试用例所在包 为 引导类所在包的子包,或者与引导类所在包的包名相同时,测试类中

@SpringBootTest()注解中可以省略参数

 

 

 

复制代码
 1 package com.haifei.springboot5test;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.boot.test.context.SpringBootTest;
 7 import org.springframework.test.context.junit4.SpringRunner;
 8 
 9 /**
10  * UserService的测试类
11  */
12 @RunWith(SpringRunner.class)
13 @SpringBootTest
14 public class UserServiceTest {
15 
16     @Autowired //注入待测试的对象
17     private UserService userService;
18 
19     @Test
20     public void testAdd(){
21         userService.add();
22     }
23 
24 }
复制代码

 

posted @   yub4by  阅读(78)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示