java-junit
Skill này làm gì
Công cụ này giúp bạn viết test cho mã Java bằng JUnit 5. Nó hướng dẫn thực hành tốt nhất để kiểm thử các chức năng thường gặp, và cả các test dùng nhiều dữ liệu đầu vào khác nhau.
Khi nào nên dùng
- ✓Khi bạn muốn đảm bảo mã Java của mình hoạt động đúng.
- ✓Khi bạn đang học viết test tự động cho dự án.
- ✓Khi bạn muốn kiểm thử mã với nhiều giá trị đầu vào mà không phải lặp lại code.
Liên kết skill
https://github.com/github/awesome-copilot/blob/main/skills/java-junit/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 để viết một test cho hàm tính tổng, kiểm tra xem 2+2 có bằng 4 không?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Công cụ giải thích cách tổ chức test, dùng các annotation của JUnit để thiết lập và chạy test, và kiểm tra kết quả có đúng như mong đợi không. Nó cũng chỉ cách dùng các nguồn dữ liệu khác nhau để kiểm thử mã với nhiều tình huống.
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.
JUnit 5+ Best Practices
Your goal is to help me write effective unit tests with JUnit 5, covering both standard and data-driven testing approaches.
Project Setup
- Use a standard Maven or Gradle project structure.
- Place test source code in
src/test/java. - Include dependencies for
junit-jupiter-api,junit-jupiter-engine, andjunit-jupiter-paramsfor parameterized tests. - Use build tool commands to run tests:
mvn testorgradle test.
Test Structure
- Test classes should have a
Testsuffix, e.g.,CalculatorTestfor aCalculatorclass. - Use
@Testfor test methods. - Follow the Arrange-Act-Assert (AAA) pattern.
- Name tests using a descriptive convention, like
methodName_should_expectedBehavior_when_scenario. - Use
@BeforeEachand@AfterEachfor per-test setup and teardown. - Use
@BeforeAlland@AfterAllfor per-class setup and teardown (must be static methods). - Use
@DisplayNameto provide a human-readable name for test classes and methods.
Standard Tests
- Keep tests focused on a single behavior.
- Avoid testing multiple conditions in one test method.
- Make tests independent and idempotent (can run in any order).
- Avoid test interdependencies.
Data-Driven (Parameterized) Tests
- Use
@ParameterizedTestto mark a method as a parameterized test. - Use
@ValueSourcefor simple literal values (strings, ints, etc.). - Use
@MethodSourceto refer to a factory method that provides test arguments as aStream,Collection, etc. - Use
@CsvSourcefor inline comma-separated values. - Use
@CsvFileSourceto use a CSV file from the classpath. - Use
@EnumSourceto use enum constants.
Assertions
- Use the static methods from
org.junit.jupiter.api.Assertions(e.g.,assertEquals,assertTrue,assertNotNull). - For more fluent and readable assertions, consider using a library like AssertJ (
assertThat(...).is...). - Use
assertThrowsorassertDoesNotThrowto test for exceptions. - Group related assertions with
assertAllto ensure all assertions are checked before the test fails. - Use descriptive messages in assertions to provide clarity on failure.
Mocking and Isolation
- Use a mocking framework like Mockito to create mock objects for dependencies.
- Use
@Mockand@InjectMocksannotations from Mockito to simplify mock creation and injection. - Use interfaces to facilitate mocking.
Test Organization
- Group tests by feature or component using packages.
- Use
@Tagto categorize tests (e.g.,@Tag("fast"),@Tag("integration")). - Use
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)and@Orderto control test execution order when strictly necessary. - Use
@Disabledto temporarily skip a test method or class, providing a reason. - Use
@Nestedto group tests in a nested inner class for better organization and structure.
Câu hỏi thường gặp
Unit test là gì?
Đó là một bài test kiểm tra một phần nhỏ của mã (thường là một hàm hoặc phương thức) một cách độc lập, để đảm bảo nó hoạt động đúng như mong đợi.
“Data-driven” test là gì?
Đây là những test chạy cùng một logic nhiều lần nhưng với các bộ dữ liệu đầu vào và đầu ra khác nhau. Cách này tránh lặp lại mã.
Tôi có cần biết nhiều về Java để dùng công cụ này không?
Có kiến thức Java cơ bản sẽ hữu ích, nhưng công cụ được thiết kế để giải thích các khái niệm kiểm thử rõ ràng, ngay cả với người mới bắt đầu.
Chi tiết
Mô tả gốc của tác giả
Get best practices for JUnit 5 unit testing, including data-driven tests
repo: github/awesome-copilot
Độ phổ biến
- Sao GitHub
- 40k
- Vị trí xếp hạng
- #7256
- 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.