mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+219
-247
@@ -1,249 +1,221 @@
|
||||
---
|
||||
description: This rule provides comprehensive best practices and coding standards for using the Mockito library in Java projects. It covers code organization, patterns, performance, security, testing, and common pitfalls to enhance test reliability and maintainability.
|
||||
globs: *.java
|
||||
description: This guide provides definitive best practices for writing robust, maintainable, and readable unit tests using Mockito with JUnit 5, focusing on modern patterns and common pitfalls.
|
||||
globs: **/*.java
|
||||
---
|
||||
# Mockito Best Practices and Coding Standards
|
||||
|
||||
This document provides comprehensive guidelines for using the Mockito library effectively in Java projects. It covers various aspects, including code organization, common patterns, performance considerations, security, testing, and common pitfalls.
|
||||
|
||||
## Library Information:
|
||||
|
||||
- Name: Mockito
|
||||
- Tags: testing, java, mocking, unit-testing
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
### 1.1 Directory Structure
|
||||
|
||||
- **Standard Maven/Gradle Structure:** Follow the standard Maven or Gradle project structure.
|
||||
- `src/main/java`: Production code.
|
||||
- `src/test/java`: Test code.
|
||||
- `src/test/resources`: Test resources (e.g., data files).
|
||||
- **Package Structure:** Mirror the package structure of your production code in your test code.
|
||||
- Example: If your production code is in `com.example.service`, your test code should be in `com.example.service` (or a subpackage like `com.example.service.tests`).
|
||||
|
||||
### 1.2 File Naming Conventions
|
||||
|
||||
- **Test Class Naming:**
|
||||
- Use the same name as the class being tested, followed by `Test` or `IT` (for integration tests).
|
||||
- Examples: `UserServiceTest`, `ProductRepositoryIT`.
|
||||
- **Test Method Naming:**
|
||||
- Use descriptive names that clearly indicate what is being tested.
|
||||
- Consider using the pattern `should_[expectedBehavior]_when_[condition]`.
|
||||
- Example: `should_returnUser_when_userExists()`.
|
||||
|
||||
### 1.3 Module Organization
|
||||
|
||||
- **Single Module:** For small projects, a single module is usually sufficient.
|
||||
- **Multi-Module Projects:** For larger projects, consider breaking down the project into modules based on functionality.
|
||||
- Example: `core`, `service`, `repository`, `api`.
|
||||
- Ensure that test code for each module resides within that module.
|
||||
|
||||
### 1.4 Component Architecture
|
||||
|
||||
- **Layered Architecture:** A common and effective architecture is layered architecture (e.g., presentation, service, data access).
|
||||
- **Test Each Layer:** Write unit tests for each layer in isolation using Mockito to mock dependencies.
|
||||
- **Integration Tests:** Write integration tests to verify the interactions between layers.
|
||||
|
||||
### 1.5 Code Splitting Strategies
|
||||
|
||||
- **Test-Driven Development (TDD):** Write tests before writing production code. This naturally leads to smaller, testable units of code.
|
||||
- **Refactoring:** Regularly refactor your code to improve its structure and testability.
|
||||
- **Extract Methods:** If a method is too complex to test easily, extract smaller, more focused methods.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### 2.1 Design Patterns Specific to Mockito
|
||||
|
||||
- **Mock Object:** The core pattern in Mockito is the Mock Object pattern, where dependencies are replaced with controlled test doubles.
|
||||
- **Dependency Injection:** Use dependency injection to make your code testable. Mockito is most effective when dependencies are easily replaceable with mocks.
|
||||
|
||||
### 2.2 Recommended Approaches for Common Tasks with Mockito
|
||||
|
||||
- **Mocking Dependencies:** Use `@Mock` annotation for cleaner mock creation.
|
||||
- **Injecting Mocks:** Use `@InjectMocks` to automatically inject mocks into the class under test.
|
||||
- **Verifying Interactions:** Use `verify()` to ensure that expected method calls occurred.
|
||||
- **Stubbing Method Calls:** Use `when(...).thenReturn(...)` to define the return values of mocked method calls.
|
||||
- **Capturing Arguments:** Use `ArgumentCaptor` to capture and assert arguments passed to mocked methods.
|
||||
|
||||
### 2.3 Anti-patterns and Code Smells to Avoid
|
||||
|
||||
- **Over-mocking:** Avoid mocking classes that are easy to instantiate and use directly (e.g., value objects, simple data structures).
|
||||
- **Testing Implementation Details:** Focus on testing behavior rather than implementation details. Tests should not break when you refactor code internally.
|
||||
- **Ignoring Test Failures:** Investigate and fix test failures promptly. Don't ignore failing tests.
|
||||
- **Writing Untestable Code:** Design code with testability in mind. Avoid tightly coupled code and dependencies.
|
||||
- **Misusing Spies:** Use spies sparingly. If you need to spy on a class, it might be a sign that the class has too many responsibilities.
|
||||
- **Chaining `when()` calls excessively:** If you have long chains of `when()` calls, it might indicate that your class under test is doing too much.
|
||||
- **Using `reset()` frequently:** Frequent use of `reset()` might suggest that your tests are not properly isolated or that your mocks are holding too much state.
|
||||
|
||||
### 2.4 State Management Best Practices
|
||||
|
||||
- **Isolated Tests:** Each test should be independent of other tests. Avoid sharing state between tests.
|
||||
- **Fresh Mocks:** Create fresh mock objects for each test using `@BeforeEach` or `@Before` (JUnit 4).
|
||||
- **Avoid Static State:** Minimize the use of static variables or shared mutable state that could affect test results.
|
||||
|
||||
### 2.5 Error Handling Patterns
|
||||
|
||||
- **Expected Exceptions:** Use `@Test(expected = Exception.class)` (JUnit 4) or `assertThrows()` (JUnit 5) to verify that expected exceptions are thrown.
|
||||
- **Exception Handling in Mocks:** Use `when(...).thenThrow(...)` to simulate exceptions thrown by mocked dependencies.
|
||||
- **Verification of Exception Handling:** If the code under test catches exceptions, verify that appropriate error handling logic is executed (e.g., logging, retries).
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1 Optimization Techniques
|
||||
|
||||
- **Lazy Initialization:** Initialize mocks only when they are needed to reduce startup time.
|
||||
- **Efficient Stubbing:** Stub only the methods that are actually used in the test case.
|
||||
- **Avoid Excessive Verification:** Verify only the interactions that are relevant to the test case.
|
||||
|
||||
### 3.2 Memory Management
|
||||
|
||||
- **Limited Mock Scope:** Keep the scope of mock objects as narrow as possible (e.g., within a method). This allows them to be garbage collected sooner.
|
||||
- **Avoid Large Mocks:** Avoid creating mocks that consume large amounts of memory (e.g., mocks with many fields or methods).
|
||||
|
||||
### 3.3 Rendering Optimization (N/A)
|
||||
|
||||
- Mockito is primarily a testing framework and does not directly involve rendering.
|
||||
|
||||
### 3.4 Bundle Size Optimization (N/A)
|
||||
|
||||
- Mockito is a testing dependency and is not included in the production bundle.
|
||||
|
||||
### 3.5 Lazy Loading Strategies (Not Directly Applicable)
|
||||
|
||||
- Lazy loading is typically used for loading data or resources. Mockito doesn't directly use lazy loading patterns, although you can lazily initialize mocks if necessary.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
### 4.1 Common Vulnerabilities and How to Prevent Them
|
||||
|
||||
- **Unintended Side Effects:** Ensure that mocked methods do not introduce unintended side effects that could compromise security.
|
||||
- **Sensitive Data Exposure:** Avoid logging or storing sensitive data in mock objects.
|
||||
|
||||
### 4.2 Input Validation Best Practices
|
||||
|
||||
- **Validate Inputs in Production Code:** Mockito tests should verify that input validation is performed correctly in the production code.
|
||||
- **Simulate Invalid Inputs:** Use Mockito to simulate invalid inputs and verify that the code handles them appropriately.
|
||||
|
||||
### 4.3 Authentication and Authorization Patterns
|
||||
|
||||
- **Mock Authentication/Authorization Services:** Mock authentication and authorization services to test different security scenarios (e.g., authorized vs. unauthorized users).
|
||||
- **Verify Access Control:** Use Mockito to verify that access control checks are performed correctly in the production code.
|
||||
|
||||
### 4.4 Data Protection Strategies
|
||||
|
||||
- **Encryption:** If sensitive data is used in mock objects, ensure that it is encrypted.
|
||||
- **Data Masking:** Use data masking techniques to protect sensitive data in mock objects.
|
||||
|
||||
### 4.5 Secure API Communication
|
||||
|
||||
- **Mock API Clients:** Mock API clients to simulate different API responses, including error conditions.
|
||||
- **Verify Secure Communication:** Use Mockito to verify that secure communication protocols (e.g., HTTPS) are used when interacting with APIs.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
### 5.1 Unit Testing Strategies
|
||||
|
||||
- **Test Individual Units:** Unit tests should focus on testing individual units of code (e.g., classes, methods) in isolation.
|
||||
- **Mock Dependencies:** Use Mockito to mock dependencies and control their behavior.
|
||||
- **Test All Scenarios:** Write tests for all possible scenarios, including normal cases, edge cases, and error conditions.
|
||||
|
||||
### 5.2 Integration Testing Approaches
|
||||
|
||||
- **Test Interactions Between Units:** Integration tests should focus on testing the interactions between different units of code.
|
||||
- **Use Real Dependencies (If Possible):** Use real dependencies whenever possible to ensure that the integration works correctly.
|
||||
- **Mock External Systems:** Mock external systems (e.g., databases, APIs) to control their behavior and simulate different scenarios.
|
||||
|
||||
### 5.3 End-to-End Testing Recommendations (Beyond Mockito)
|
||||
|
||||
- **Use UI Testing Frameworks:** Use UI testing frameworks (e.g., Selenium, Cypress) to test the entire application flow.
|
||||
- **Test User Interactions:** End-to-end tests should simulate real user interactions to ensure that the application works as expected.
|
||||
|
||||
### 5.4 Test Organization Best Practices
|
||||
|
||||
- **Separate Test Classes:** Create separate test classes for each class being tested.
|
||||
- **Descriptive Test Names:** Use descriptive test names that clearly indicate what is being tested.
|
||||
- **Arrange, Act, Assert:** Follow the Arrange, Act, Assert (AAA) pattern in your tests.
|
||||
- **Keep Tests Short and Focused:** Keep tests short and focused on testing a single aspect of the code.
|
||||
|
||||
### 5.5 Mocking and Stubbing Techniques
|
||||
|
||||
- **`@Mock` Annotation:** Use `@Mock` to create mock objects.
|
||||
- **`@InjectMocks` Annotation:** Use `@InjectMocks` to automatically inject mock objects into the class under test.
|
||||
- **`when(...).thenReturn(...)`:** Use `when(...).thenReturn(...)` to stub method calls.
|
||||
- **`verify(...)`:** Use `verify(...)` to verify that method calls occurred.
|
||||
- **`ArgumentCaptor`:** Use `ArgumentCaptor` to capture and assert arguments passed to mocked methods.
|
||||
- **`doReturn(...).when(...)`:** Use `doReturn(...).when(...)` when stubbing void methods or methods that are called multiple times.
|
||||
- **`thenAnswer(...)`:** Use `thenAnswer(...)` to provide custom logic for stubbing method calls.
|
||||
- **`thenThrow(...)`:** Use `thenThrow(...)` to simulate exceptions thrown by mocked methods.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1 Frequent Mistakes Developers Make
|
||||
|
||||
- **Mocking Everything:** Mocking simple classes can lead to brittle tests. Use real objects when possible.
|
||||
- **Testing Implementation Details:** Focus on testing behavior, not implementation.
|
||||
- **Ignoring Test Failures:** Always investigate and fix test failures promptly.
|
||||
- **Not Using Argument Matchers Properly:** When using argument matchers, ensure that all arguments are either exact values or matchers.
|
||||
- **Forgetting to Initialize Mocks:** Ensure that mocks are initialized using `MockitoAnnotations.initMocks(this)` or `@ExtendWith(MockitoExtension.class)` (JUnit 5).
|
||||
|
||||
### 6.2 Edge Cases to Be Aware Of
|
||||
|
||||
- **Final Classes and Methods:** Mockito cannot mock final classes or methods by default. Consider using the Mockito inline mock maker or PowerMock (though PowerMock should be used as a last resort due to complexity).
|
||||
- **Static Methods:** Mockito cannot mock static methods by default. Consider using the Mockito inline mock maker or PowerMock.
|
||||
- **Equals and HashCode:** Mockito can have issues with `equals()` and `hashCode()` methods, especially when using argument matchers. Be careful when mocking classes that rely heavily on these methods.
|
||||
- **Order of Interactions:** Mockito verifies interactions in the order they occur. Be mindful of the order of method calls when writing tests.
|
||||
|
||||
### 6.3 Version-Specific Issues
|
||||
|
||||
- **Mockito 2 vs. Mockito 3 vs. Mockito 4/5:** Be aware of the differences between Mockito versions. Some features may be deprecated or added in newer versions.
|
||||
- **Java Version Compatibility:** Ensure that your Mockito version is compatible with your Java version.
|
||||
|
||||
### 6.4 Compatibility Concerns
|
||||
|
||||
- **JUnit Version:** Ensure that your Mockito version is compatible with your JUnit version.
|
||||
- **Other Testing Frameworks:** Mockito can be used with other testing frameworks, but ensure that there are no compatibility issues.
|
||||
- **IDE Integration:** Ensure that your IDE has proper support for Mockito and JUnit.
|
||||
|
||||
### 6.5 Debugging Strategies
|
||||
|
||||
- **Print Statements:** Use print statements to debug the code and verify that the mock objects are behaving as expected.
|
||||
- **IDE Debugger:** Use the IDE debugger to step through the code and inspect the mock objects.
|
||||
- **Mockito Spy:** Use a Mockito spy to partially mock a class and observe its behavior.
|
||||
- **Verbose Logging:** Use verbose logging to see all method calls and their return values.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1 Recommended Development Tools
|
||||
|
||||
- **IDE:** IntelliJ IDEA, Eclipse, or Visual Studio Code.
|
||||
- **Build Tool:** Maven or Gradle.
|
||||
- **Testing Framework:** JUnit 4 or JUnit 5.
|
||||
- **Code Coverage Tool:** JaCoCo or SonarQube.
|
||||
|
||||
### 7.2 Build Configuration Best Practices
|
||||
|
||||
- **Dependency Management:** Use Maven or Gradle to manage Mockito dependencies.
|
||||
- **Test Dependencies:** Declare Mockito as a test dependency to ensure that it is not included in the production code.
|
||||
- **Plugin Configuration:** Configure Maven or Gradle plugins to run tests and generate code coverage reports.
|
||||
|
||||
### 7.3 Linting and Formatting
|
||||
|
||||
- **Code Style:** Follow a consistent code style (e.g., Google Java Style, Checkstyle).
|
||||
- **Linting Rules:** Use linting tools (e.g., PMD, SpotBugs) to enforce coding standards and identify potential issues.
|
||||
- **Formatting:** Use code formatters (e.g., IntelliJ IDEA formatter, Eclipse formatter) to automatically format your code.
|
||||
|
||||
### 7.4 Deployment Best Practices (N/A)
|
||||
|
||||
- Mockito is a testing dependency and is not deployed with the production code.
|
||||
|
||||
### 7.5 CI/CD Integration
|
||||
|
||||
- **Automated Builds:** Configure your CI/CD system (e.g., Jenkins, CircleCI, GitHub Actions) to automatically build and test your code.
|
||||
- **Test Execution:** Run unit tests and integration tests as part of the CI/CD pipeline.
|
||||
- **Code Coverage:** Generate code coverage reports as part of the CI/CD pipeline and set thresholds for code coverage.
|
||||
- **Static Analysis:** Perform static analysis as part of the CI/CD pipeline to identify potential issues.
|
||||
|
||||
## Conclusion
|
||||
|
||||
By following these best practices, you can write more effective and maintainable tests using Mockito. Remember to focus on testing behavior, avoid over-mocking, and keep your tests short and focused.
|
||||
# Mockito Best Practices
|
||||
|
||||
Mockito is the indispensable mocking framework for Java unit testing. Adhere to these guidelines to ensure your tests are clear, reliable, and resistant to refactoring.
|
||||
|
||||
## 1. Standardize Test Setup with JUnit 5 & Annotations
|
||||
|
||||
Always use JUnit 5's `@ExtendWith` for Mockito integration and leverage annotations for mock initialization. This keeps your setup concise and automatic.
|
||||
|
||||
❌ **BAD: Manual setup or old JUnit 4 runner**
|
||||
```java
|
||||
// JUnit 4: @RunWith(MockitoJUnitRunner.class) - Obsolete
|
||||
// Manual mock creation
|
||||
UserRepository repo = mock(UserRepository.class);
|
||||
UserService service = new UserService(repo);
|
||||
```
|
||||
|
||||
✅ **GOOD: JUnit 5 `@ExtendWith` and annotations**
|
||||
```java
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class UserServiceTest {
|
||||
@Mock
|
||||
UserRepository userRepository; // Collaborator
|
||||
|
||||
@InjectMocks
|
||||
UserService userService; // System Under Test (SUT)
|
||||
|
||||
// ... tests
|
||||
}
|
||||
```
|
||||
|
||||
## 2. One Test, One Behavior
|
||||
|
||||
Each test method must verify a single, distinct behavior. This makes tests easier to debug and provides clear documentation.
|
||||
|
||||
❌ **BAD: Multiple unrelated assertions in one test**
|
||||
```java
|
||||
@Test
|
||||
void processOrder_createsUser_addsPoints_sendsEmail() {
|
||||
// ... setup
|
||||
orderService.placeOrder(orderRequest);
|
||||
verify(userRepository).save(any(User.class));
|
||||
verify(loyaltyService).addPoints(anyString(), anyInt());
|
||||
verify(notificationService).sendEmail(anyString(), anyString());
|
||||
}
|
||||
```
|
||||
|
||||
✅ **GOOD: Focused tests**
|
||||
```java
|
||||
@Test
|
||||
void shouldSaveUserWhenOrderIsPlaced() {
|
||||
orderService.placeOrder(orderRequest);
|
||||
verify(userRepository).save(any(User.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddLoyaltyPointsForEligibleUsers() {
|
||||
when(loyaltyPolicy.isEligible(any())).thenReturn(true);
|
||||
orderService.placeOrder(orderRequest);
|
||||
verify(loyaltyService).addPoints(anyString(), anyInt());
|
||||
}
|
||||
```
|
||||
|
||||
## 3. Verify Outcomes, Not Implementation Details
|
||||
|
||||
Only verify interactions that are part of the contract being tested. Avoid verifying internal method calls or private state.
|
||||
|
||||
❌ **BAD: Verifying internal helper method calls**
|
||||
```java
|
||||
// Assuming OrderService.placeOrder() internally calls calculateTax()
|
||||
@Test
|
||||
void shouldCalculateTaxWhenPlacingOrder() {
|
||||
orderService.placeOrder(orderRequest);
|
||||
verify(taxCalculator).calculateTax(any()); // Tax calculation is an internal detail
|
||||
}
|
||||
```
|
||||
|
||||
✅ **GOOD: Verifying the observable outcome**
|
||||
```java
|
||||
@Test
|
||||
void shouldReturnOrderWithCorrectTotalIncludingTax() {
|
||||
when(taxCalculator.calculateTax(any())).thenReturn(BigDecimal.valueOf(10));
|
||||
Order result = orderService.placeOrder(orderRequest);
|
||||
assertEquals(BigDecimal.valueOf(110), result.getTotalAmount()); // Verify the final amount
|
||||
}
|
||||
```
|
||||
|
||||
## 4. Handle `UnnecessaryStubbingException` Correctly
|
||||
|
||||
Mockito's strict stubbing (default with `MockitoExtension`) catches unused stubs. This prevents dead code in tests. Resolve it by removing unused stubs. Use `lenient()` only when absolutely necessary (e.g., in parameterized tests where some stubs might not be hit in all cases).
|
||||
|
||||
❌ **BAD: Silencing all strictness with `@MockitoSettings(strictness = Strictness.LENIENT)`**
|
||||
```java
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT) // Hides potential issues
|
||||
class MyServiceTest {
|
||||
// ...
|
||||
@Test
|
||||
void someTest() {
|
||||
when(mockDependency.someMethod()).thenReturn("value"); // This stub is never used
|
||||
// ... test logic that doesn't call someMethod()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
✅ **GOOD: Remove unused stubs or use `lenient()` for specific stubs**
|
||||
```java
|
||||
@ExtendWith(MockitoExtension.class) // Strictness is default
|
||||
class MyServiceTest {
|
||||
// ...
|
||||
@Test
|
||||
void someTest() {
|
||||
// No unused stubs here
|
||||
// If a stub is conditionally used, make it lenient:
|
||||
Mockito.lenient().when(mockDependency.optionalMethod()).thenReturn("optional");
|
||||
// ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 5. Use `ArgumentCaptor` for Complex Argument Verification
|
||||
|
||||
When you need to inspect the exact values of objects passed to a mock, `ArgumentCaptor` is your tool.
|
||||
|
||||
❌ **BAD: Generic `any()` when specific content matters**
|
||||
```java
|
||||
verify(invoiceRepository).save(any(Invoice.class)); // Only checks type
|
||||
```
|
||||
|
||||
✅ **GOOD: Capture and assert on the actual object's state**
|
||||
```java
|
||||
import org.mockito.ArgumentCaptor;
|
||||
// ...
|
||||
@Test
|
||||
void shouldSaveInvoiceWithCorrectDetails() {
|
||||
// ... call SUT method
|
||||
ArgumentCaptor<Invoice> captor = ArgumentCaptor.forClass(Invoice.class);
|
||||
verify(invoiceRepository).save(captor.capture());
|
||||
Invoice savedInvoice = captor.getValue();
|
||||
assertEquals("ORD-123", savedInvoice.getOrderId());
|
||||
assertEquals(BigDecimal.valueOf(150.00), savedInvoice.getAmount());
|
||||
}
|
||||
```
|
||||
|
||||
## 6. Prefer Mocks over Spies
|
||||
|
||||
Use mocks for external, unmanaged dependencies where you control all behavior. Use spies only when you need partial real behavior of an object.
|
||||
|
||||
❌ **BAD: Spying on simple dependencies that should be fully mocked**
|
||||
```java
|
||||
@Spy
|
||||
UserService userService = new UserService(userRepository); // Unnecessary complexity
|
||||
```
|
||||
|
||||
✅ **GOOD: Mocking dependencies, using `@InjectMocks` for SUT**
|
||||
```java
|
||||
@Mock
|
||||
UserRepository userRepository;
|
||||
|
||||
@InjectMocks
|
||||
UserService userService; // SUT is a real instance, dependencies are mocked
|
||||
```
|
||||
|
||||
## 7. Use Parameterized Tests for Similar Scenarios
|
||||
|
||||
Avoid duplicate test methods for different inputs. Parameterized tests keep your test suite DRY and maintainable.
|
||||
|
||||
❌ **BAD: Repetitive tests for different inputs**
|
||||
```java
|
||||
@Test void shouldReturnBronzeFor0Points() { /* ... */ }
|
||||
@Test void shouldReturnSilverFor1000Points() { /* ... */ }
|
||||
```
|
||||
|
||||
✅ **GOOD: Single parameterized test**
|
||||
```java
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
// ...
|
||||
class LoyaltyServiceTest {
|
||||
@InjectMocks
|
||||
LoyaltyService loyaltyService;
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"0, BRONZE",
|
||||
"1000, SILVER",
|
||||
"5000, GOLD"
|
||||
})
|
||||
void shouldReturnCorrectTierForPoints(int points, String expectedTier) {
|
||||
String actualTier = loyaltyService.calculateTier(points);
|
||||
assertEquals(expectedTier, actualTier);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 8. Mock Final Classes, Static Methods, and Constructors (with Caution)
|
||||
|
||||
Mockito 5 (and newer) supports mocking these without extra dependencies. Use this feature judiciously for testing legacy code or third-party APIs that were not designed for testability. Avoid it for your own code; design for testability instead.
|
||||
|
||||
```java
|
||||
// Example (use sparingly for legacy/third-party code)
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.when;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
@Test
|
||||
void shouldMockStaticMethod() {
|
||||
try (MockedStatic<MyStaticUtil> mockedStatic = mockStatic(MyStaticUtil.class)) {
|
||||
when(MyStaticUtil.getConstantValue()).thenReturn("MOCKED_VALUE");
|
||||
assertEquals("MOCKED_VALUE", MyStaticUtil.getConstantValue());
|
||||
}
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user