Skip to content
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

Move Junit 5 assertThrows to last statement in lamdba #590

Merged
merged 9 commits into from
Sep 6, 2024

Conversation

shivanisky
Copy link
Collaborator

@shivanisky shivanisky commented Aug 30, 2024

What's changed?

In multi line assertThrows move assertThrows to last statement only

What's your motivation?

Currently the Junit 4 to 5 recipe migrates the @test junit 4 annotation to wrap the entire method to assertThrows when typically we want it on the last statement.

Anyone you would like to review specifically?

@timtebeek

Have you considered any alternatives or workarounds?

Yes - may be modifying the existing recipe is better? Rather than providing an extra recipe to move existing assertThrows with multi line lamdbas to the last statement only, but we can hook this recipe in optionally, so ... maybe not. Maybe that can be an optional argument in the existing recipe - move applyAssertThrowsToLast and the user can set it to true if they are ok to take a hit of compilation errors in favour of better usage of junit 5 features and correctness.

Any additional context

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

@shivanisky shivanisky changed the title Initial stab at recipe to move assertThrows to last statement in lamdba Initial stab at recipe to move Junit 5 assertThrows to last statement in lamdba Aug 30, 2024
@shivanisky shivanisky marked this pull request as draft August 30, 2024 02:59
@timtebeek
Copy link
Contributor

Hi @shivanisky ; Great start! I've pushed a few quick changes to get you on a path to get this mostly working; then from here we'd have to guard against the cases where we might do harm, which would be the more interesting challenge of this recipe. There's a few such cases described in #194 that we might want to cover here. Thanks again for exploring if we an actually see this through in a mostly reliable way.

@timtebeek timtebeek added the enhancement New feature or request label Aug 30, 2024
@shivanisky
Copy link
Collaborator Author

shivanisky commented Aug 31, 2024

Hi @shivanisky ; Great start! I've pushed a few quick changes to get you on a path to get this mostly working; then from here we'd have to guard against the cases where we might do harm, which would be the more interesting challenge of this recipe. There's a few such cases described in #194 that we might want to cover here. Thanks again for exploring if we an actually see this through in a mostly reliable way.

Thank you so much for your optimisations and feature adds! Its looking very close now :).

Regarding the final/non final variable it seems it's a bit overkill to do as it's a rare case?

I'm thinking, what if we hook this into the main junit 4 to 5 recipe which migrates the assertThrows if an optional parameter is specified in yml file to run it? I would like to do it this way.

@shivanisky
Copy link
Collaborator Author

shivanisky commented Sep 2, 2024

Added case for variable declaration ie Throwable ex = assertThrows(....) seems to be working well.

@shivanisky
Copy link
Collaborator Author

I think no point in putting a lot of effort for non final variables case - I am not seeing it across multiple repos with thousands of test cases and over 500 files. Have put in warning in description regarding this rare case, and I think better that people can use it if they want to. Eventually, can hook in to the main junit 5 migration recipe with argument to optionally use this, so that scope is limited to migrating the junit 4 @test annotation only rather than touching existing assertThrows lamdas.

@shivanisky shivanisky marked this pull request as ready for review September 6, 2024 03:25
@shivanisky shivanisky changed the title Initial stab at recipe to move Junit 5 assertThrows to last statement in lamdba Move Junit 5 assertThrows to last statement in lamdba Sep 6, 2024
@sambsnyd sambsnyd merged commit 541e480 into openrewrite:main Sep 6, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

JUnit 5 Assertions.assertThrows should only contain a single statement
3 participants