Nội dung chi tiết
1. Nguyên tắc Unit Test API
Mỗi API cần test các nhóm case cơ bản:
-
Validate input
-
Auth / permission
-
Business logic
-
Repository interaction
-
Exception handling
-
Logging
-
HTTP status & response
2. Ví dụ API mẫu
Controller:
DTO:
Service:
3. Cấu trúc test chuẩn
Dependency:
Controller test dùng:
-
@WebMvcTest
-
MockMvc
-
@MockBean
4. Test Validate Input
Case cần có:
-
thiếu field bắt buộc
-
email sai format
-
request null
5. Test Auth / Permission
Giả sử API cần login:
Test chưa login:
6. Test Business Logic (Service)
7. Test Exception Handling
Giả sử email trùng:
Test:
8. Test Logging
Giả sử service có log:
Test log:
(Thực tế có thể dùng LogCaptor)
9. Test HTTP Response
10. Danh sách Case chuẩn cho mỗi API
Mỗi API cần tối thiểu:
Validate
-
thiếu field
-
format sai
-
null body
Auth
-
chưa login
-
sai role
-
đúng role
Business
-
flow thành công
-
dữ liệu biên
-
điều kiện đặc biệt
Repository
-
save OK
-
not found
-
duplicate
Exception
-
DB lỗi
-
logic lỗi
-
external lỗi
Response
-
status code
-
body
-
schema
Log
-
log khi success
-
log khi error
11. Checklist Unit Test API
-
Test validate
-
Test auth
-
Test service
-
Mock repo
-
Test exception
-
Test response
-
Verify interaction
-
No DB thật
-
No network
Không có bình luận nào để hiển thị
Không có bình luận nào để hiển thị