Skip to content

Use FindImports and dependency-aware framework detection in ReplaceMockitoTestExecutionListener#925

Merged
timtebeek merged 6 commits intomainfrom
find-imports-refactor
Mar 9, 2026
Merged

Use FindImports and dependency-aware framework detection in ReplaceMockitoTestExecutionListener#925
timtebeek merged 6 commits intomainfrom
find-imports-refactor

Conversation

@steve-aom-elliott
Copy link
Contributor

@steve-aom-elliott steve-aom-elliott commented Mar 4, 2026

Summary

  • Replace manual import iteration in detectFramework() with FindImports search visitors
  • Add targetFramework option for dependency-based framework detection when no imports are present
  • Add three YAML wrapper recipes (...ForJUnit5, ...ForJUnit4, ...ForTestNG) gated by ModuleHasDependency preconditions
  • Update Mockito1to3Migration to use the wrapper recipes in priority order

When a file has @TestExecutionListeners(MockitoTestExecutionListener.class) but no JUnit/TestNG imports (e.g., an abstract base class with only @Mock fields), the recipe previously defaulted to JUnit 5 blindly. Now the YAML wrappers check project dependencies to determine the correct framework.

Import detection still takes priority over targetFramework, so a TestNG file in a JUnit 5 project still gets TestNG treatment.

Test plan

  • All existing tests pass (16 tests)
  • New test: targetFramework fallback when file has no framework imports
  • New test: import detection overrides targetFramework

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Mar 4, 2026
@steve-aom-elliott steve-aom-elliott moved this from In Progress to Ready to Review in OpenRewrite Mar 4, 2026
@steve-aom-elliott steve-aom-elliott moved this from Ready to Review to In Progress in OpenRewrite Mar 4, 2026
@steve-aom-elliott steve-aom-elliott marked this pull request as draft March 4, 2026 15:27
@steve-aom-elliott
Copy link
Contributor Author

I want to check over one other thing regarding making sure it can detect correctly when we don't actually have any JUnit or TestNG annotations in the current class.

@steve-aom-elliott steve-aom-elliott changed the title Use FindImports for framework detection in ReplaceMockitoTestExecutionListener Use FindImports and dependency-aware framework detection in ReplaceMockitoTestExecutionListener Mar 4, 2026
@steve-aom-elliott steve-aom-elliott marked this pull request as ready for review March 4, 2026 16:08
@steve-aom-elliott steve-aom-elliott moved this from In Progress to Ready to Review in OpenRewrite Mar 4, 2026
…cutionListener

Replace manual import loop in detectFramework() with FindImports
search visitors for cleaner, more idiomatic framework detection.
Add a targetFramework @option that YAML wrapper recipes set based on
ModuleHasDependency preconditions. Import-based detection still takes
priority; the option is a fallback for files with no framework imports.

Three YAML wrapper recipes gate on JUnit 5, JUnit 4, and TestNG
dependencies respectively, replacing the single bare reference in
Mockito1to3Migration.
Since JUnit 5 (org.junit.jupiter..*) is matched first, any remaining
org.junit..* import is necessarily JUnit 4.
@timtebeek timtebeek changed the title Use FindImports and dependency-aware framework detection in ReplaceMockitoTestExecutionListener Use FindImports and dependency-aware framework detection in ReplaceMockitoTestExecutionListener Mar 9, 2026
@timtebeek timtebeek merged commit 8654451 into main Mar 9, 2026
1 check passed
@timtebeek timtebeek deleted the find-imports-refactor branch March 9, 2026 18:33
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Mar 9, 2026
@steve-aom-elliott
Copy link
Contributor Author

Going to put in another small PR because when the targetFramework changed from junit5 to jupiter, the switch statement didn't get updated. I'll just merge that once that's passing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request junit mockito

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants