Skip to content

feat: implement Java assertion removal transformer#1295

Merged
misrasaurabh1 merged 4 commits intoomni-javafrom
feat/java-remove-asserts-transformer
Feb 3, 2026
Merged

feat: implement Java assertion removal transformer#1295
misrasaurabh1 merged 4 commits intoomni-javafrom
feat/java-remove-asserts-transformer

Conversation

@misrasaurabh1
Copy link
Contributor

Summary

Add a robust Java assert removal transformer to convert AI-generated unit tests into regression tests. This removes assertion statements while preserving function calls, enabling behavioral verification by comparing outputs between original and optimized code.

Transformation Examples

// INPUT: JUnit 5
assertEquals(55, calculator.fibonacci(10));
// OUTPUT
Object _cf_result1 = calculator.fibonacci(10);

// INPUT: AssertJ (fluent API)
assertThat(calc.add(2, 3)).isEqualTo(5);
// OUTPUT
Object _cf_result1 = calc.add(2, 3);

// INPUT: assertThrows with lambda
assertThrows(IllegalArgumentException.class, () -> calculator.divide(1, 0));
// OUTPUT
try { calculator.divide(1, 0); } catch (Exception _cf_ignored1) {}

Key Features

  • JUnit 5 support: assertEquals, assertTrue, assertThrows, assertAll, etc.
  • JUnit 4 support: org.junit.Assert.*
  • AssertJ support: Fluent assertions like assertThat().isEqualTo()
  • TestNG & Hamcrest support: Additional framework compatibility
  • Framework auto-detection: Detects framework from imports
  • Nested assertion handling: Properly handles assertAll with nested assertions
  • Non-assertion preservation: Keeps setup code, Mockito mocks, variable declarations

Files Changed

File Action
codeflash/languages/java/remove_asserts.py CREATE - Main transformer
codeflash/languages/java/instrumentation.py MODIFY - Integration point
codeflash/languages/java/__init__.py MODIFY - Export new functions
tests/test_java_assertion_removal.py CREATE - 57 comprehensive tests

Test plan

  • All 57 tests pass with exact string equality assertions
  • Pre-commit hooks pass (ruff check, ruff format)
  • Integration with instrument_generated_java_test verified

🤖 Generated with Claude Code

misrasaurabh1 and others added 3 commits February 3, 2026 09:02
Add a robust Java assert removal transformer to convert generated unit
tests into regression tests. This removes assertion statements while
preserving function calls, enabling behavioral verification by comparing
outputs between original and optimized code.

Key features:
- Support for JUnit 5 assertions (assertEquals, assertTrue, assertThrows, etc.)
- Support for JUnit 4 assertions (org.junit.Assert.*)
- Support for AssertJ fluent assertions (assertThat().isEqualTo())
- Support for TestNG and Hamcrest assertions
- Framework auto-detection from imports
- Handles assertAll grouped assertions
- Preserves non-assertion code (setup, Mockito mocks, etc.)
- 57 comprehensive tests with exact string equality assertions

Co-Authored-By: Claude Opus 4.5 <[email protected]>
The log statement was using `func_name` which is only defined in the
Java block, not the JavaScript block.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 3, 2026

⚡️ Codeflash found optimizations for this PR

📄 13% (0.13x) speedup for _add_behavior_instrumentation in codeflash/languages/java/instrumentation.py

⏱️ Runtime : 4.73 milliseconds 4.18 milliseconds (best of 210 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch feat/java-remove-asserts-transformer).

Static Badge

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 3, 2026

⚡️ Codeflash found optimizations for this PR

📄 236% (2.36x) speedup for JavaAssertTransformer._detect_framework in codeflash/languages/java/remove_asserts.py

⏱️ Runtime : 15.6 milliseconds 4.64 milliseconds (best of 135 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch feat/java-remove-asserts-transformer).

Static Badge

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 3, 2026

⚡️ Codeflash found optimizations for this PR

📄 22% (0.22x) speedup for JavaAssertTransformer._find_junit_assertions in codeflash/languages/java/remove_asserts.py

⏱️ Runtime : 9.88 milliseconds 8.08 milliseconds (best of 100 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch feat/java-remove-asserts-transformer).

Static Badge

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 3, 2026

⚡️ Codeflash found optimizations for this PR

📄 21% (0.21x) speedup for JavaAssertTransformer._find_hamcrest_assertions in codeflash/languages/java/remove_asserts.py

⏱️ Runtime : 7.67 milliseconds 6.34 milliseconds (best of 184 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch feat/java-remove-asserts-transformer).

Static Badge

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 3, 2026

⚡️ Codeflash found optimizations for this PR

📄 33% (0.33x) speedup for JavaAssertTransformer._find_fluent_chain_end in codeflash/languages/java/remove_asserts.py

⏱️ Runtime : 1.92 milliseconds 1.45 milliseconds (best of 141 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch feat/java-remove-asserts-transformer).

Static Badge

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 3, 2026

⚡️ Codeflash found optimizations for this PR

📄 12% (0.12x) speedup for JavaAssertTransformer._extract_target_calls in codeflash/languages/java/remove_asserts.py

⏱️ Runtime : 3.81 milliseconds 3.41 milliseconds (best of 211 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch feat/java-remove-asserts-transformer).

Static Badge

- Update receiver extraction pattern to handle constructor calls
- Fix test expectation for behavior mode instrumentation

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@misrasaurabh1 misrasaurabh1 merged commit 2fd26ba into omni-java Feb 3, 2026
16 of 27 checks passed
@misrasaurabh1 misrasaurabh1 deleted the feat/java-remove-asserts-transformer branch February 3, 2026 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant