Java Regression Test Generator
Automatically generate regression tests for Java code based on version changes.
Overview
This skill analyzes changes between old and new versions of Java code and generates regression tests that ensure previously tested behavior still works while covering new or modified functionality. It preserves correctness guarantees from existing tests and suggests updates when old tests become invalid.
How to Use
Provide:
- Old version: The previous version of Java code
- New version: The updated version of Java code
- Existing tests: Test classes or methods for the old version
- Framework (optional): JUnit 5 (default) or TestNG
The skill will:
- Analyze changes between versions
- Identify modified, added, or removed functionality
- Generate regression tests for the new code
- Suggest updates for invalid existing tests
- Produce executable test code ready for integration
Test Generation Workflow
Step 1: Analyze Code Changes
Compare old and new versions to identify changes:
Method-level changes:
- Signature changes (parameters, return type)
- Logic modifications (algorithm, error handling)
- New methods added
- Methods removed
Class-level changes:
- Field additions or removals
- Constructor changes
- Inheritance modifications
- Access modifier changes
Dependency changes:
- New dependencies injected
- Dependency injection patterns added
- External API changes
Step 2: Classify Change Types
Categorize each change to determine test generation strategy:
Type 1: Signature Changes
- Parameter added/removed/reordered
- Return type changed
- Exception declarations modified
Type 2: Behavioral Changes
- Algorithm optimized (same behavior)
- Error handling added
- Validation logic added
Type 3: Structural Changes
- New methods/classes added
- Methods/classes removed
- Refactoring (extract method, rename)
Type 4: Dependency Changes
- Constructor injection added
- Dependencies replaced
- Mock requirements changed
Step 3: Determine Test Strategy
For each change type, decide on test generation approach:
| Change Type | Strategy |
|---|---|
| Parameter added | Migrate existing tests + generate new parameter tests |
| Return type changed | Update assertions + add new type-specific tests |
| New method | Generate full test coverage (normal, edge, error cases) |
| Logic optimized | Preserve existing tests + add performance/edge tests |
| Error handling added | Preserve existing tests + add exception tests |
| Method removed | Mark tests as obsolete, suggest removal |
| Dependency injection | Update test setup with mocks |
See change_patterns.md for detailed strategies.
Step 4: Generate Regression Tests
Create test code following these principles:
Preserve existing behavior:
- Keep all tests that verify unchanged functionality
- Migrate tests when signatures change but behavior is preserved
- Maintain test names and structure when possible
Cover new functionality:
- Generate tests for new methods
- Add tests for new parameters or return types
- Test new error conditions
Ensure quality:
- Use appropriate assertions
- Include setup/teardown code
- Add mocks for dependencies
- Follow framework conventions (JUnit 5 or TestNG)
Step 5: Handle Invalid Tests
When existing tests become invalid, suggest updates:
Scenario 1: Signature changed
// Old test (invalid)
@Test
public void testCalculateDiscount() {
assertEquals(10, calculator.calculateDiscount(100));
}
// Suggested update
@Test
public void testCalculateDiscount_WithDefaultPercent() {
// Updated to use new signature with default value
assertEquals(10, calculator.calculateDiscount(100, 10));
}
Scenario 2: Return type changed
// Old test (invalid)
@Test
public void testGetUser() {
User user = service.getUser("123");
assertEquals("John", user.getName());
}
// Suggested update
@Test
public void testGetUser() {
Optional<User> userOpt = service.getUser("123");
assertTrue(userOpt.isPresent());
assertEquals("John", userOpt.get().getName());
}
Scenario 3: Method removed
// Old test (obsolete)
// @Test
// public void testDeprecatedMethod() {
// service.deprecatedMethod();
// }
// OBSOLETE: deprecatedMethod() removed in new version
// Consider: Check if functionality moved to another method
Example: Parameter Added
Old Code:
public class DiscountCalculator {
public int calculateDiscount(int price) {
return price * 10 / 100; // Fixed 10% discount
}
}
New Code:
public class DiscountCalculator {
public int calculateDiscount(int price, int discountPercent) {
return price * discountPercent / 100; // Configurable discount
}
}
Existing Test:
@Test
public void testCalculateDiscount() {
DiscountCalculator calc = new DiscountCalculator();
assertEquals(10, calc.calculateDiscount(100));
}
@Test
public void testCalculateDiscount_LargeAmount() {
DiscountCalculator calc = new DiscountCalculator();
assertEquals(100, calc.calculateDiscount(1000));
}
Generated Regression Tests:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class DiscountCalculatorTest {
private DiscountCalculator calculator = new DiscountCalculator();
// Migrated tests - preserve original behavior with default 10%
@Test
public void testCalculateDiscount_WithDefaultPercent() {
// Migrated from testCalculateDiscount
// Uses 10% to match old behavior
assertEquals(10, calculator.calculateDiscount(100, 10));
}
@Test
public void testCalculateDiscount_LargeAmount_WithDefaultPercent() {
// Migrated from testCalculateDiscount_LargeAmount
assertEquals(100, calculator.calculateDiscount(1000, 10));
}
// New tests for added parameter
@Test
public void testCalculateDiscount_WithCustomPercent() {
assertEquals(25, calculator.calculateDiscount(100, 25));
assertEquals(50, calculator.calculateDiscount(200, 25));
}
@Test
public void testCalculateDiscount_WithZeroPercent() {
assertEquals(0, calculator.calculateDiscount(100, 0));
}
@Test
public void testCalculateDiscount_WithHundredPercent() {
assertEquals(100, calculator.calculateDiscount(100, 100));
}
@Test
public void testCalculateDiscount_WithNegativePercent() {
// Edge case: negative discount
assertEquals(-10, calculator.calculateDiscount(100, -10));
}
}
Analysis:
- Change type: Parameter added (discountPercent)
- Strategy: Migrate existing tests + generate new parameter tests
- Preserved: Original test logic with default 10% discount
- Added: Tests for various discount percentages (0%, 25%, 100%, negative)
Example: New Method Added
Old Code:
public class UserService {
private UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
public User getUser(String id) {
return repository.findById(id);
}
}
New Code:
public class UserService {
private UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
public User getUser(String id) {
return repository.findById(id);
}
// New method added
public List<User> getUsersByRole(String role) {
return repository.findAll().stream()
.filter(u -> u.getRole().equals(role))
.collect(Collectors.toList());
}
}
Existing Tests:
@Test
public void testGetUser() {
UserRepository mockRepo = mock(UserRepository.class);
when(mockRepo.findById("123")).thenReturn(new User("123", "John", "admin"));
UserService servi