Commit eb3d51a
fix: prevent duplicate and wrong test-to-function associations in Java
Fixed two critical bugs in Java test discovery that caused incorrect
test-to-function mappings:
## Bug 1: Duplicate Test Associations
**Problem**: The function_map contained duplicate keys (both func.name and
func.qualified_name pointing to the same object). When iterating over the map
in Strategy 1, each function was processed twice, causing duplicate test
associations.
**Example**:
- function_map['fibonacci'] → fibonacci function
- function_map['Calculator.fibonacci'] → fibonacci function (same object!)
When matching testFibonacci, it would match TWICE and get added TWICE.
**Fix**: Added duplicate check in Strategy 1 (line 117):
```python
if func_info.qualified_name not in matched:
matched.append(func_info.qualified_name)
```
## Bug 2: Wrong Test Associations
**Problem**: Strategy 3 (class naming convention) was too broad. It would
associate ALL methods in a class with EVERY test in that class's test file.
**Example**:
- CalculatorTest has testFibonacci and testSumRange
- Strategy 3 strips "Test" → "Calculator"
- Finds ALL methods in Calculator class (fibonacci, sumRange)
- Associates BOTH with EVERY test
Result:
- testFibonacci incorrectly associated with sumRange
- testSumRange incorrectly associated with fibonacci
**Fix**: Made Strategy 3 a fallback - only runs if no matches found yet:
```python
if not matched and test_method.class_name:
```
## Impact
**Before**:
```
Calculator.fibonacci → 3 tests:
- testFibonacci
- testFibonacci (duplicate!)
- testSumRange (wrong!)
Calculator.sumRange → 3 tests:
- testFibonacci (wrong!)
- testSumRange
- testSumRange (duplicate!)
```
**After**:
```
Calculator.fibonacci → 1 test:
- testFibonacci ✓
Calculator.sumRange → 1 test:
- testSumRange ✓
```
## Testing
✅ All 24 test discovery tests pass
✅ Verified with real Java project (java-test-project)
✅ Each test now correctly maps to only its target function
This fix is critical for optimization correctness - wrong test associations
would cause incorrect behavior verification and benchmarking results.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 7fc14fd commit eb3d51a
1 file changed
Lines changed: 5 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
| 118 | + | |
| 119 | + | |
119 | 120 | | |
120 | 121 | | |
121 | 122 | | |
| |||
137 | 138 | | |
138 | 139 | | |
139 | 140 | | |
140 | | - | |
| 141 | + | |
141 | 142 | | |
142 | | - | |
| 143 | + | |
| 144 | + | |
143 | 145 | | |
144 | 146 | | |
145 | 147 | | |
| |||
0 commit comments