Skip to content

AssertTrueInstanceofToAssertInstanceOf doesn't add the static import for assertInstanceOf if missing #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
henri-tremblay opened this issue May 18, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@henri-tremblay
Copy link

What version of OpenRewrite are you using?

rewrite-testing-frameworks:3.8.0

How are you running OpenRewrite?

Maven

What is the smallest, simplest way to reproduce the problem?

Run the recipe on a test with assertTrue(e instanceof List) where the assert is a static import

import static org.junit.jupiter.api.Assertions.assertTrue;

The code is correctly updated and the assertTrue import is correctly removed but import static org.junit.jupiter.api.Assertions.assertInstanceOf; is not added.

@henri-tremblay henri-tremblay added the bug Something isn't working label May 18, 2025
@timtebeek
Copy link
Member

Thanks for the report! It looks like there were some missing types related to how the JavaTemplate was used, which in the past we've seen lead to failure to add imports as the maybeAddImport requires correct type information. I've addressed this in 4de7603, and hope this results in a fix for you as well; would you mind trying again with the latest snapshot version when you have a chance?

@henri-tremblay
Copy link
Author

It works. My only complain it that it moves to a start import if there are 3 explicit imports. For me it should just add import static org.junit.jupiter.api.Assertions.assertInstanceOf;

@timtebeek
Copy link
Member

Hmm that's odd; typically we detect with a project wide tally at which point to switch from individual imports to star imports. Is the project already consistently using more than three before it uses star imports? Or might there be a couple cases where it's eagerly using star imports for fewer items?

If necessary you can define a particular style to use for a project, even temporarily, and then run a recipe like https://docs.openrewrite.org/recipes/java/orderimports to make them consistent. From that point on the detected number of imports should match the desired.

@henri-tremblay
Copy link
Author

Interesting. You can try with EasyMock. There's only one usage of import static org.junit.jupiter.api.Assertions.* in the project. Everything else are explicit imports. However, import static org.easymock.EasyMock.* is used all over the place. So maybe that erratic style causes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

No branches or pull requests

2 participants