当前位置: 首页 > news >正文

Spring Boot Web应用开发:测试

在Spring Boot中,测试是开发过程的一个重要部分,它确保你的应用按预期工作,并且可以帮助你在早期发现和修复问题。Spring Boot提供了多种便捷的测试工具,使得编写和运行测试案例变得简单。

Spring Boot测试简介

Spring Boot支持集成测试和单元测试。它提供了一个spring-boot-starter-test起步依赖,里面包含了常用的测试库,如JUnit、Spring Test & Spring Boot Test、AssertJ、Hamcrest、Mockito、JsonPath等。

在Spring Boot中,可以使用@SpringBootTest注解来编写集成测试,它会加载应用程序的完整上下文。而对于单元测试,可以使用@MockBean@DataJpaTest@WebMvcTest等注解来创建所需的上下文。

编写和运行测试案例

测试案例通常位于项目的src/test/java目录下。你可以使用JUnit框架来编写测试方法,并使用断言来验证结果是否符合预期。

示例:编写一个简单的单元测试

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;@SpringBootTest
public class CalculatorTests {@Autowiredprivate CalculatorService calculatorService;@Testpublic void testAdd() {assertThat(calculatorService.add(2, 3)).isEqualTo(5);}
}@Service
public class CalculatorService {public int add(int a, int b) {return a + b;}
}

在上面的例子中,我们创建了一个CalculatorService类,以及一个测试类CalculatorTests来测试add方法。使用了assertThat方法和isEqualTo来验证结果。

测试REST API

测试REST API时,Spring Boot提供了MockMvc来模拟HTTP请求,并验证响应。@WebMvcTest注解用于单元测试Spring MVC应用程序,它只加载相关的MVC组件。

示例:测试REST API

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;@WebMvcTest(controllers = GreetingController.class)
public class GreetingControllerTests {@Autowiredprivate MockMvc mockMvc;@Testpublic void testGreeting() throws Exception {mockMvc.perform(get("/greeting")).andExpect(status().isOk()).andExpect(content().string("Hello, World!"));}
}@RestController
public class GreetingController {@GetMapping("/greeting")public String greeting() {return "Hello, World!";}
}

在这个例子中,GreetingControllerTests使用MockMvc发送了一个GET请求到/greeting端点,并验证了响应状态码是200(OK),以及响应内容是"Hello, World!"。

通过这样的测试,可以确保你的REST API按预期工作。Spring Boot的测试支持使得编写和运行测试变得非常简单,有助于维护和提高代码质量。

http://www.lryc.cn/news/492305.html

相关文章:

  • 服务器数据恢复—光纤存储FC硬盘数据恢复案例
  • Android Binder技术概览
  • 09 —— Webpack搭建开发环境
  • 深度学习模型:卷积神经网络(CNN)
  • Flask 自定义路由转换器
  • 【淘汰9成NLP面试者的高频面题】LSTM中的tanh和sigmoid分别用在什么地方?为什么?
  • gocv调用opencv添加中文乱码的解决方案
  • org.apache.log4j的日志记录级别和基础使用Demo
  • IC数字后端实现之大厂IC笔试真题(经典时序计算和时序分析题)
  • java centos 离线使用sherpa-onnx文字转语音TTS
  • Android 11 三方应用监听关机广播ACTION_SHUTDOWN
  • OpenHarmony-3.驱动HDF
  • 《白帽子讲Web安全》13-14章
  • CSS - CSS One-Line
  • gitlab ssh-key 绑定
  • wordpress使用Markdown语法写的文章图片显示不正常,记录一次折腾之旅
  • 从零开始学GeoServer源码(二)添加支持arcgis切片功能
  • WPF异步UI交互功能的实现方法
  • 网络基础 - 地址篇
  • # [Unity] 【游戏开发】Unity开发基础2-Unity脚本编程基础详解
  • Milvus实操
  • 35 基于单片机的精确电压表DA-AD转换
  • JDBC 设置 PostgreSQL 查询中 any(?) 的参数
  • 【11-20期】Java面试进阶:深入解析核心问题与实战案例
  • C++——内存池_2
  • 如何使用PHP爬虫获取店铺详情:一篇详尽指南
  • HTML5和CSS3新增特性
  • linux运行vue编译后的项目
  • 论文阅读:A Software Platform for Manipulating theCamera Imaging Pipeline
  • 【MySQL篇】持久化和非持久化统计信息的深度剖析(第一篇,总共六篇)