Skip to content

Conversation

amishra-u
Copy link
Contributor

What's changed?

Recipe to close unclosed static mocks in tests.

What's your motivation?

In JUnit4, when a timeout is specified, the test executes in a separate thread. Mockito stores static mocks in thread-local storage, which works fine in this context. However, in JUnit5, tests typically run on the main thread, even with timeout, leading to issues when static mocks are not explicitly closed.

This discrepancy causes tests to fail during migration from JUnit4 to JUnit5. Additionally, unclosed static mocks can leak into subsequent tests, leading to unpredictable failures and making debugging significantly more difficult.

Anyone you would like to review specifically?

@timtebeek

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite May 24, 2025
Copy link
Contributor

@greg-at-moderne greg-at-moderne left a comment

Choose a reason for hiding this comment

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

I am hardly an SME on this topic. But the description you have provided and the test case contributed make sense to me. I have reviewed the code from a general perspective and have no comments. LGTM.

I am gonna give my colleagues a chance to submit some feedback they might have. Otherwise let's merge.

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite May 27, 2025
@greg-at-moderne
Copy link
Contributor

Thanks for the contribution!

void shouldWrapMockStaticInTryWithResources() {
//language=java
rewriteRun(
spec -> spec.afterTypeValidationOptions(TypeValidation.builder().identifiers(false).build()), // TODO Remove escape hatch
Copy link
Member

Choose a reason for hiding this comment

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

Tried for a bit if we could remove this ignore, but it seemed a bit tricky. May try again when fresh, although I'm not opposed to merging as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can achieve this by splitting the logic into two passes. The first pass will scan the file to determine what changes are needed, and the second pass will apply those changes. This approach is similar to scanningRecipe, but we don’t need to use scanningRecipe here since all the required information is available within the same file.

However, this might not be worth the effort, given that the functionality is already implemented.

@timtebeek
Copy link
Member

timtebeek commented May 28, 2025

I've pushed one last change to include this by default, given the challenge outlined above, such that folks do not need to seek this out explicitly for a recipe run. Thanks again!

@timtebeek timtebeek merged commit 97be007 into openrewrite:main May 28, 2025
1 of 2 checks passed
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite May 28, 2025
@timtebeek timtebeek added recipe Recipe request mockito labels May 28, 2025
@timtebeek
Copy link
Member

Saw some unfortunate effects here after including it, so I've had to take it out from being included d1e6bbb

You'll likely see the same where we now add a teardown to any nested class, which would need to be adjusted:
https://ge.openrewrite.org/s/5jwlz5wzde35m/tests/task/:test/details/org.openrewrite.java.testing.jmockit.JMockitMockUpToMockitoTest/mockUpAtSetUpWithTearDownTest()?top-execution=1

I'll keep the recipe in for this release as I didn't want to revert your work and make it troublesome to include, but any help with the fix would be appreciated.

@amishra-u
Copy link
Contributor Author

Saw some unfortunate effects here after including it, so I've had to take it out from being included d1e6bbb

You'll likely see the same where we now add a teardown to any nested class, which would need to be adjusted: https://ge.openrewrite.org/s/5jwlz5wzde35m/tests/task/:test/details/org.openrewrite.java.testing.jmockit.JMockitMockUpToMockitoTest/mockUpAtSetUpWithTearDownTest()?top-execution=1

I'll keep the recipe in for this release as I didn't want to revert your work and make it troublesome to include, but any help with the fix would be appreciated.

I have already fixed all those issues in our internal version. I will publish the new PR with updated changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mockito recipe Recipe request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants