java-unit-test
Generate Java Unit Tests with JUnit 5 and Mockito
Automatically generate comprehensive unit tests for Java projects using JUnit 5 and Mockito. This skill helps you write test cases that cover happy paths, edge cases, and error scenarios while maintaining best practices.
تنزيل ZIP المهارة
رفع في Claude
اذهب إلى Settings → Capabilities → Skills → Upload skill
فعّل وابدأ الاستخدام
اختبرها
استخدام "java-unit-test". Generate a test class for a BankAccount class with deposit, withdraw, and checkBalance methods
النتيجة المتوقعة:
- @ExtendWith(MockitoExtension.class) class BankAccountTest { @InjectMocks private BankAccount bankAccount; @Test void shouldDepositAmountSuccessfully() { // Arrange double amount = 1000.0; // Act bankAccount.deposit(amount); // Assert assertEquals(1000.0, bankAccount.getBalance()); } @Test void shouldWithdrawAmountSuccessfully() { // Arrange bankAccount.deposit(1000.0); double amount = 500.0; // Act bankAccount.withdraw(amount); // Assert assertEquals(500.0, bankAccount.getBalance()); } }
استخدام "java-unit-test". Test an email service with happy path and exception handling
النتيجة المتوقعة:
- @ExtendWith(MockitoExtension.class) class EmailServiceTest { @InjectMocks private EmailService emailService; @Mock private EmailClient emailClient; @Test void shouldSendEmailSuccessfully() { // Arrange Email email = new Email("test@example.com", "Hello", "Test body"); when(emailClient.send(any())).thenReturn(true); // Act boolean result = emailService.send(email); // Assert assertTrue(result); verify(emailClient, times(1)).send(any()); } @Test void shouldThrowExceptionWhenEmailIsNull() { // Act & Assert assertThrows(IllegalArgumentException.class, () -> emailService.send(null)); } }
التدقيق الأمني
آمنAll static analysis findings are false positives. The detected patterns are documentation text and comments, not actual code. This is a legitimate Java unit testing skill with no security concerns.
عوامل الخطر
⚡ يحتوي على سكربتات (1)
درجة الجودة
ماذا يمكنك بناءه
New Java Developer
Generate unit tests for new classes following best practices and project conventions
Legacy Codebase Improvement
Add tests to existing classes to improve coverage and prevent regressions
Test Generation During Code Review
Quickly generate tests for code changes during pull request reviews
جرّب هذه الموجهات
Generate a JUnit 5 test class for the UserService class. Include @ExtendWith(MockitoExtension.class), mock dependencies, and test the createUser method with valid input. Use @Test annotation and proper assertions.
Generate comprehensive test cases for the calculateInterest method. Test happy path, boundary conditions (zero, negative numbers), and exception handling for invalid inputs.
Create parameterized tests for the filterProducts method with different category and price range combinations. Use @ParameterizedTest and @ValueSource or @MethodSource.
Generate test class with @InjectMocks, @Mock annotations. Configure mock behavior using when().thenReturn(), and add verify() calls to check that dependencies are called with correct parameters.
أفضل الممارسات
- Use Arrange-Act-Assert pattern for clear test structure
- Name tests with clear method name + scenario + expected result
- Keep tests independent and repeatable without execution order dependencies
- Avoid deep stubs and focus on testing observable behavior
- Mock only external dependencies, not helper methods
- Use specific assertions rather than generic ones like assertNotNull
تجنب
- Testing private methods directly - test through public interfaces instead
- Writing slow tests that include sleep, real network calls, or file I/O
- Over-mocking leading to brittle tests that break on minor implementation changes
- Testing implementation details instead of business behavior
- Having tests that depend on execution order or randomness
- Leaving unused imports and imports in wrong order