使用@SpringBootTest注解进行单元测试
作者:快乐柠檬 时间:2021-09-27 04:02:04
概述
@SpringBootTest注解是SpringBoot自1.4.0版本开始引入的一个用于测试的注解。基本用法如下:
1. 添加Maven依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. 编写启动入口类
@SpringBootApplication
public class StartUpApplication {
public static void main(String[] args) {
SpringApplication.run(StartUpApplication.class, args);
}
}
3. 编写Controller类
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Hello Spring Boot,Index!";
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return "Spring Boot Test Demo!";
}
}
4. 编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StartUpApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerTest {
/**
* @LocalServerPort 提供了 @Value("${local.server.port}") 的代替
*/
@LocalServerPort
private int port;
private URL base;
@Autowired
private TestRestTemplate restTemplate;
@Before
public void setUp() throws Exception {
String url = String.format("http://localhost:%d/", port);
System.out.println(String.format("port is : [%d]", port));
this.base = new URL(url);
}
/**
* 向"/test"地址发送请求,并打印返回结果
* @throws Exception
*/
@Test
public void test1() throws Exception {
ResponseEntity<String> response = this.restTemplate.getForEntity(
this.base.toString() + "/test", String.class, "");
System.out.println(String.format("测试结果为:%s", response.getBody()));
}
其中,classes属性指定启动类,SpringBootTest.WebEnvironment.RANDOM_PORT经常和测试类中@LocalServerPort一起在注入属性时使用。会随机生成一个端口号。
总结
我们发现,随着Spring boot 版本的提升,单元测试变得更简单了。
来源:https://blog.csdn.net/limenghua9112/article/details/79694849
标签:@SpringBootTest,单元测试
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现FTP上传到服务器
2022-10-07 10:28:58
23种设计模式(12)java模版方法模式
2021-10-24 11:00:31
![](https://img.aspxhome.com/file/2023/6/94306_0s.jpg)
Java数组越界问题实例解析
2023-10-25 18:16:23
C#实现winform中RichTextBox在指定光标位置插入图片的方法
2022-04-04 01:50:16
java中dart类详细讲解
2022-01-23 13:01:13
SpringBoot2.0 ZipKin示例代码
2022-11-25 00:24:40
![](https://img.aspxhome.com/file/2023/9/70989_0s.png)
Android Insets相关知识总结
2023-04-24 09:08:23
![](https://img.aspxhome.com/file/2023/5/109155_0s.png)
java实现截取PDF指定页并进行图片格式转换功能
2023-08-24 02:58:56
详解SpringBoot获得Maven-pom中版本号和编译时间戳
2022-11-27 19:45:27
![](https://img.aspxhome.com/file/2023/6/91476_0s.png)
Springboot中如何使用Jackson
2021-07-29 03:27:34
![](https://img.aspxhome.com/file/2023/7/76487_0s.png)
zookeeper概述图文详解
2021-12-07 15:46:03
![](https://img.aspxhome.com/file/2023/6/71946_0s.png)
C#数据类型转换(显式转型、隐式转型、强制转型)
2021-11-24 13:44:25
![](https://img.aspxhome.com/file/2023/1/72461_0s.jpg)
Java模拟计算机的整数乘积计算功能示例
2022-01-30 19:25:51
![](https://img.aspxhome.com/file/2023/8/75678_0s.png)
Java实现马踏棋盘算法
2023-03-05 04:30:46
![](https://img.aspxhome.com/file/2023/4/76254_0s.jpg)
java实现word文档转pdf并添加水印的方法详解
2023-05-26 02:59:18
![](https://img.aspxhome.com/file/2023/9/62139_0s.png)
C#编程实现向并口设备发送指令、获取并口设备的状态
2023-02-02 02:56:39
Java中文件操作功能小结
2023-06-20 03:32:28
![](https://img.aspxhome.com/file/2023/8/87348_0s.webp)
C#日期格式强制转换方法(推荐)
2022-09-19 10:24:57
Opencv光流运动物体追踪详解
2023-06-21 11:55:31
![](https://img.aspxhome.com/file/2023/4/91994_0s.jpg)
C#实现日期格式转换的公共方法类实例
2023-05-16 10:06:54