spring-boot-testing
Skill này làm gì
Công cụ này giúp bạn test mã Spring Boot bằng cách chọn phương pháp test phù hợp nhất với điều bạn muốn kiểm tra. Nó dùng các công cụ hiện đại để test dễ viết và rõ ràng hơn.
Khi nào nên dùng
- ✓Khi bạn muốn kiểm tra một “endpoint” (cổng giao tiếp web) có hoạt động đúng không.
- ✓Khi bạn muốn kiểm tra phần chương trình lưu và đọc dữ liệu có đúng không.
- ✓Khi bạn cần test cách chương trình giao tiếp với các dịch vụ khác qua internet.
Liên kết skill
https://github.com/github/awesome-copilot/blob/main/skills/spring-boot-testing/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Làm thế nào để kiểm tra xem mã lưu dữ liệu vào cơ sở dữ liệu có hoạt động đúng không?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Công cụ phân tích điều bạn muốn kiểm tra và đề xuất cách tiếp cận tốt nhất, ví dụ dùng `@WebMvcTest` để test phần giao diện web hoặc `@DataJpaTest` để test phần dữ liệu. Nó cũng khuyên dùng các thư viện như AssertJ để viết test dễ đọc hơn.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Spring Boot Testing
This skill provides expert guide for testing Spring Boot 4 applications with modern patterns and best practices.
Core Principles
- Test Pyramid: Unit (fast) > Slice (focused) > Integration (complete)
- Right Tool: Use the narrowest slice that gives you confidence
- AssertJ Style: Fluent, readable assertions over verbose matchers
- Modern APIs: Prefer MockMvcTester and RestTestClient over legacy alternatives
Which Test Slice?
| Scenario | Annotation | Reference |
|---|---|---|
| Controller + HTTP semantics | @WebMvcTest |
references/webmvctest.md |
| Repository + JPA queries | @DataJpaTest |
references/datajpatest.md |
| REST client + external APIs | @RestClientTest |
references/restclienttest.md |
| JSON (de)serialization | @JsonTest |
references/test-slices-overview.md |
| Full application | @SpringBootTest |
references/test-slices-overview.md |
Test Slices Reference
- references/test-slices-overview.md - Decision matrix and comparison
- references/webmvctest.md - Web layer with MockMvc
- references/datajpatest.md - Data layer with Testcontainers
- references/restclienttest.md - REST client testing
Testing Tools Reference
- references/mockmvc-tester.md - AssertJ-style MockMvc (3.2+)
- references/mockmvc-classic.md - Traditional MockMvc (pre-3.2)
- references/resttestclient.md - Spring Boot 4+ REST client
- references/mockitobean.md - Mocking dependencies
Assertion Libraries
- references/assertj-basics.md - Scalars, strings, booleans, dates
- references/assertj-collections.md - Lists, Sets, Maps, arrays
Testcontainers
- references/testcontainers-jdbc.md - PostgreSQL, MySQL, etc.
Test Data Generation
- references/instancio.md - Generate complex test objects (3+ properties)
Performance & Migration
- references/context-caching.md - Speed up test suites
- references/sb4-migration.md - Spring Boot 4.0 changes
Quick Decision Tree
Testing a controller endpoint?
Yes → @WebMvcTest with MockMvcTester
Testing repository queries?
Yes → @DataJpaTest with Testcontainers (real DB)
Testing business logic in service?
Yes → Plain JUnit + Mockito (no Spring context)
Testing external API client?
Yes → @RestClientTest with MockRestServiceServer
Testing JSON mapping?
Yes → @JsonTest
Need full integration test?
Yes → @SpringBootTest with minimal context config
Spring Boot 4 Highlights
- RestTestClient: Modern alternative to TestRestTemplate
- @MockitoBean: Replaces @MockBean (deprecated)
- MockMvcTester: AssertJ-style assertions for web tests
- Modular starters: Technology-specific test starters
- Context pausing: Automatic pausing of cached contexts (Spring Framework 7)
Testing Best Practices
Code Complexity Assessment
When a method or class is too complex to test effectively:
- Analyze complexity - If you need more than 5-7 test cases to cover a single method, it's likely too complex
- Recommend refactoring - Suggest breaking the code into smaller, focused functions
- User decision - If the user agrees to refactor, help identify extraction points
- Proceed if needed - If the user decides to continue with the complex code, implement tests despite the difficulty
Example of refactoring recommendation:
// Before: Complex method hard to test
public Order processOrder(OrderRequest request) {
// Validation, discount calculation, payment, inventory, notification...
// 50+ lines of mixed concerns
}
// After: Refactored into testable units
public Order processOrder(OrderRequest request) {
validateOrder(request);
var order = createOrder(request);
applyDiscount(order);
processPayment(order);
updateInventory(order);
sendNotification(order);
return order;
}
Avoid Code Redundancy
…
Câu hỏi thường gặp
“Test Pyramid” (Kim tự tháp kiểm thử) là gì?
Đây là cách tổ chức test: nhiều test nhỏ và nhanh (đơn vị), một số test trung bình (slice), và ít test lớn, chậm hơn (tích hợp).
Vì sao dùng AssertJ?
AssertJ giúp test dễ đọc và dễ viết hơn, dùng cách diễn đạt gần với ngôn ngữ tự nhiên để kiểm tra kết quả có đúng không.
“Test Slices” là gì?
Đây là các test tập trung vào một phần cụ thể của ứng dụng, như chỉ lớp mạng hoặc chỉ lớp dữ liệu, giúp test nhanh hơn và chính xác hơn.
Chi tiết
Mô tả gốc của tác giả
Expert Spring Boot 4 testing specialist that selects the best Spring Boot testing techniques for your situation with Junit 6 and AssertJ.
repo: github/awesome-copilot
Độ phổ biến
- Sao GitHub
- 40k
- Vị trí xếp hạng
- #7431
- Giấy phép
- MIT
- Cập nhật
- 15/10/2026
Bình luận
- …
Thêm skill từ kho mã này
acquire-codebase-knowledge
40kGiúp agent của bạn hiểu và ghi tài liệu cho một dự án mã nguồn có sẵn. Hữu ích khi đón thành viên mới hoặc nắm tổng quan về kiến trúc.
acreadiness-assess
40kGiúp agent của bạn đánh giá mức độ sẵn sàng cho AI của dự án và tạo báo cáo. Hữu ích để biết dự án đã sẵn sàng dùng công cụ AI đến đâu.
acreadiness-generate-instructions
40kGiúp agent của bạn tạo hướng dẫn cho công cụ AI dựa trên kết quả đánh giá. Hữu ích để cải thiện cách AI làm việc với mã và khắc phục các khoảng trống.
acreadiness-policy
40kGiúp agent của bạn xác định hoặc áp dụng chính sách đánh giá dự án bằng AI. Hữu ích để chuẩn hóa cách đánh giá dự án và đảm bảo tuân thủ.
Skill tương tự
test-driven-development
311kDạy agent của bạn viết kiểm thử thất bại trước, xem nó thất bại, rồi viết đoạn mã ngắn nhất để vượt qua trước khi làm bất kỳ tính năng hay bản sửa nào.
verification-before-completion
311kDạy agent của bạn chạy kiểm tra thực tế và đọc kết quả đầu ra trước khi khẳng định một việc, bản sửa hoặc bản build đã hoàn thành hay vượt qua.
migrate-to-shoehorn
310kThay các ép kiểu rủi ro trong tệp kiểm thử bằng thư viện shoehorn, để các bài kiểm thử dùng dữ liệu một phần vẫn qua được kiểm tra kiểu.