Chuyển đến nội dung
Skillpedia
Kiểm thử40k · #7431

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.md

Sao 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

Hãy dùng skill này để giúp tôi: https://github.com/github/awesome-copilot/blob/main/skills/spring-boot-testing/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

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

  1. Test Pyramid: Unit (fast) > Slice (focused) > Integration (complete)
  2. Right Tool: Use the narrowest slice that gives you confidence
  3. AssertJ Style: Fluent, readable assertions over verbose matchers
  4. 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

Testing Tools Reference

Assertion Libraries

Testcontainers

Test Data Generation

Performance & Migration

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:

  1. Analyze complexity - If you need more than 5-7 test cases to cover a single method, it's likely too complex
  2. Recommend refactoring - Suggest breaking the code into smaller, focused functions
  3. User decision - If the user agrees to refactor, help identify extraction points
  4. 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

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự