Skip to content

Fix variable name conflict in JMockit to Mockito migration#865

Merged
timtebeek merged 3 commits intomainfrom
fix/844
Dec 3, 2025
Merged

Fix variable name conflict in JMockit to Mockito migration#865
timtebeek merged 3 commits intomainfrom
fix/844

Conversation

@knutwannheden
Copy link
Contributor

@knutwannheden knutwannheden commented Dec 3, 2025

When migrating JMockit Expectations blocks to Mockito, variables declared inside the block that have the same names as variables later in the test method now stay accessible to the generated when() calls.

Changes

  • Modified SetupStatementsRewriter to skip setup statement extraction when there's a variable name conflict
  • Updated JMockitBlockRewriter to track and preserve setup statements within mock invocation groups
  • Added wrapStatementsInBlock() to wrap both setup statements and when() calls in the same block
  • Updated setupStatementVariableNameConflict test to reflect the corrected behavior
  • Added new test setupStatementVariableUsedInResultAndTestCode for issue Still Variable name conflict in JmockitToMockito #844

Before (broken)

{
    String res = "mockedValue";
}
when(config.getValue()).thenReturn(res);  // error: res not in scope

After (fixed)

{
    String res = "mockedValue";
    when(config.getValue()).thenReturn(res);
}

When migrating JMockit `Expectations` blocks to Mockito, variables declared inside the block that have the same names as variables later in the test method now stay accessible to the generated `when()` calls.

### Changes
- Modified `SetupStatementsRewriter` to skip setup statement extraction when there's a variable name conflict
- Updated `JMockitBlockRewriter` to track and preserve setup statements within mock invocation groups
- Added `wrapStatementsInBlock()` to wrap both setup statements and `when()` calls in the same block
- Updated `setupStatementVariableNameConflict` test to reflect the corrected behavior
- Added new test `setupStatementVariableUsedInResultAndTestCode` for issue #844

### Before (broken)
```java
{
    String res = "mockedValue";
}
when(config.getValue()).thenReturn(res);  // error: res not in scope
```

### After (fixed)
```java
{
    String res = "mockedValue";
    when(config.getValue()).thenReturn(res);
}
```

Fixes:
 - #844
Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Dec 3, 2025
@timtebeek timtebeek merged commit fa95357 into main Dec 3, 2025
2 checks passed
@timtebeek timtebeek deleted the fix/844 branch December 3, 2025 15:19
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Dec 3, 2025
mergify bot added a commit to robfrank/linklift that referenced this pull request Dec 14, 2025
…rom 3.22.0 to 3.23.0 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-testing-frameworks](https://github.com/openrewrite/rewrite-testing-frameworks) from 3.22.0 to 3.23.0.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-testing-frameworks's releases](https://github.com/openrewrite/rewrite-testing-frameworks/releases).*

> 3.23.0
> ------
>
> What's Changed
> --------------
>
> * Add `@ExtendWith(MockitoExtension.class)` Test using Mockito Annotations by [`@​MBoegers`](https://github.com/MBoegers) in [openrewrite/rewrite-testing-frameworks#848](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/848)
> * Add troubleshooting context info when ParseError encountered in `GradleUseJunitJupiter` by [`@​greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-testing-frameworks#852](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/852)
> * Fix/850 testcontainers2 migration only springboot4 by [`@​Thomas-Iapichino`](https://github.com/Thomas-Iapichino) in [openrewrite/rewrite-testing-frameworks#854](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/854)
> * Fixed the expectation to be correctly spaced + formatted args by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#855](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/855)
> * Simplify redundant AssertJ chains of `hasSize` and `containsExactly` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#857](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/857)
> * Simplify AssertJ `stream().map(Function)` to `extracting(Function)` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#856](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/856)
> * `AssertThrowsOnLastStatement`: Support calls with lambda parameters by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-testing-frameworks#859](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/859)
> * Fix bugs in `HamcrestMatcherToAssertJ` and `HamcrestIsMatcherToAssertJ` by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-testing-frameworks#863](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/863)
> * `HamcrestMatcherToAssertJ`: `isNull()` and `isNotNull()` take no args by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-testing-frameworks#864](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/864)
> * Fix variable name conflict in JMockit to Mockito migration by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-testing-frameworks#865](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/865)
>
> New Contributors
> ----------------
>
> * [`@​Thomas-Iapichino`](https://github.com/Thomas-Iapichino) made their first contribution in [openrewrite/rewrite-testing-frameworks#854](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/854)
>
> **Full Changelog**: <openrewrite/rewrite-testing-frameworks@v3.22.0...v3.23.0>


Commits

* [`fe6c497`](openrewrite/rewrite-testing-frameworks@fe6c497) Update recipe documentation examples
* [`fa95357`](openrewrite/rewrite-testing-frameworks@fa95357) Fix variable name conflict in JMockit to Mockito migration ([#865](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/865))
* [`5869a99`](openrewrite/rewrite-testing-frameworks@5869a99) `HamcrestMatcherToAssertJ`: `isNull()` and `isNotNull()` take no args ([#864](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/864))
* [`6ea4619`](openrewrite/rewrite-testing-frameworks@6ea4619) Fix bugs in `HamcrestMatcherToAssertJ` and `HamcrestIsMatcherToAssertJ` ([#863](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/863))
* [`bd7ec84`](openrewrite/rewrite-testing-frameworks@bd7ec84) `AssertThrowsOnLastStatement`: Support calls with lambda parameters ([#859](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/859))
* [`959c88b`](openrewrite/rewrite-testing-frameworks@959c88b) Fix order of Hamcrest migration recipes
* [`ebb0358`](openrewrite/rewrite-testing-frameworks@ebb0358) `MigrateTruthToAssertJ` should not call `RemoveUnusedImports` unrestricted
* [`df6b865`](openrewrite/rewrite-testing-frameworks@df6b865) Skip `CsvSourceToValueSource` if textBlock attribute is used
* [`d623f54`](openrewrite/rewrite-testing-frameworks@d623f54) Simplify AssertJ `stream().map(Function)` to `extracting(Function)` ([#856](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/856))
* [`a9829fb`](openrewrite/rewrite-testing-frameworks@a9829fb) Simplify redundant AssertJ chains of `hasSize` and `containsExactly` ([#857](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/857))
* Additional commits viewable in [compare view](openrewrite/rewrite-testing-frameworks@v3.22.0...v3.23.0)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-testing-frameworks&package-manager=maven&previous-version=3.22.0&new-version=3.23.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Still Variable name conflict in JmockitToMockito

2 participants