3.14、前后端联调测试
分类: 搭建单体商城服务
前后端联调测试
前后端联调测试是确保系统功能正常的重要环节。本节将学习如何进行前后端联调测试,验证系统功能。
本节将学习:接口测试、页面功能测试、数据流验证,以及问题排查。
接口测试
Postman 测试
使用 Postman 测试 API:
-
用户注册接口
- Method: POST
- URL: http://localhost:8080/api/users/register
- Body (JSON):
{ "username": "testuser", "email": "test@example.com", "password": "password123" } -
用户登录接口
- Method: POST
- URL: http://localhost:8080/api/users/login
- Params:
- username: testuser
- password: password123
-
商品列表接口
- Method: GET
- URL: http://localhost:8080/api/products?current=1&size=10
curl 测试
# 用户注册 curl -X POST http://localhost:8080/api/users/register \ -H "Content-Type: application/json" \ -d '{ "username": "testuser", "email": "test@example.com", "password": "password123" }' # 用户登录 curl -X POST "http://localhost:8080/api/users/login?username=testuser&password=password123" # 商品列表 curl http://localhost:8080/api/products?current=1&size=10
页面功能测试
测试流程
测试检查清单
页面功能测试检查清单:
- 页面能否正常打开
- 数据能否正常显示
- 表单能否正常提交
- 错误信息能否正常显示
- 链接能否正常跳转
数据流验证
数据流测试
数据流验证步骤:
- 创建数据:通过接口或页面创建数据
- 查询数据:验证数据是否正确保存
- 更新数据:修改数据并验证
- 删除数据:删除数据并验证
数据库验证
-- 验证用户数据 SELECT * FROM user WHERE username = 'testuser'; -- 验证商品数据 SELECT * FROM product WHERE status = 1; -- 验证订单数据 SELECT o.*, oi.* FROM `order` o LEFT JOIN order_item oi ON o.id = oi.order_id WHERE o.user_id = 1;
问题排查
常见问题
常见问题及解决方案:
-
404 错误
- 检查 URL 是否正确
- 检查 Controller 路径映射
- 检查静态资源路径
-
500 错误
- 查看服务器日志
- 检查数据库连接
- 检查业务逻辑
-
数据不显示
- 检查数据库数据
- 检查 Model 绑定
- 检查 Thymeleaf 语法
日志查看
# application.yml logging: level: com.example.ecommerce: DEBUG org.springframework.web: DEBUG
测试工具
测试工具列表
官方资源
- Postman 文档:https://learning.postman.com/
- curl 文档:https://curl.se/docs/
本节小结
在本节中,我们学习了:
第一个是接口测试。 使用 Postman 和 curl 测试 API 接口。
第二个是页面功能测试。 在浏览器中测试页面功能。
第三个是数据流验证。 验证数据的创建、查询、更新、删除流程。
第四个是问题排查。 排查常见问题,查看日志。
这就是前后端联调测试。通过测试,我们可以确保系统功能正常。
在下一节,我们将总结单体应用的优缺点,为微服务化做准备。