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

Migrate Hamcrest to JUnit 5 #343

Merged
merged 41 commits into from
Dec 10, 2024
Merged

Migrate Hamcrest to JUnit 5 #343

merged 41 commits into from
Dec 10, 2024

Conversation

matusmatokpt
Copy link
Contributor

@matusmatokpt matusmatokpt commented May 23, 2023

This is a draft pull request for

matusmatokpt and others added 3 commits May 22, 2023 12:46
Work in progress implementation of a recipe which migrates Hamcrest test matchers to JUnit5 test assertions.

Signed-off-by: matus.matok <[email protected]>
@timtebeek timtebeek marked this pull request as draft May 23, 2023 12:23
@timtebeek timtebeek changed the title WIP: Add MigrateFromHamcrest recipe Migrate Hamcrest to AssertJ May 23, 2023
@timtebeek timtebeek changed the title Migrate Hamcrest to AssertJ Migrate Hamcrest to JUnit 5 May 23, 2023
@timtebeek timtebeek added the recipe Recipe request label May 23, 2023
timtebeek and others added 5 commits May 23, 2023 15:28
Added a prototype-y implementation of translation from hamcrest's equalTo to JUnit5's assertEquals. Should be easy to add more of the simple hamcrest matchers to this implementation.

\TODO the import is not being added

Signed-off-by: matus.matok <[email protected]>
matusmatokpt and others added 2 commits June 13, 2023 10:50
Another iteration of the prototype. Adapted it to be
based on the up-to-date main.
Added a proposal of how similar simple matchers could be
translated to junit assertion methods.

Signed-off-by: matus.matok <[email protected]>
@matusmatokpt
Copy link
Contributor Author

matusmatokpt commented Jun 13, 2023

Hello, got back to this. Noticed that the code base updated a little so I ammended my prototype. The tests dont run however, I am getting a bunch of error messages and I have not figured out how they are related to my changes, so if anyone could take a look, that would be appreciated.
Secondly, I addressed Tim's hints from the comments above, and added a proposal of how to add further simple matcher-to-method translations. Looking forward to your feedback. If there will be a desire to split each case (by matchers) into separate recipes then I will of course do so.
Another remark: for these simple matchers we do not need vararg methods as mentioned here #343 (comment) , due to their simple nature. Might be needed for matchers like anyOf, but we are not there yet. Those will probably need a different approach.

@timtebeek timtebeek requested a review from knutwannheden June 13, 2023 12:30
@timtebeek
Copy link
Contributor

Appreciate you getting back to this! I've asked @knutwannheden to have a look as I'm caught up in other work this week; from the failures it appears your recipe might not apply or make changes. Did you already try to step through with the debugger?

@matusmatokpt
Copy link
Contributor Author

Yes I did. Seemingly, the recipe instantiates alright, but during the run phase it fails and I don't understand why. Wanted to double check it so I inserted some console prints into the recipe and they are printed as expected when the test is run. I will add some more simple matcher-to-method cases while I wait for the review.

@timtebeek
Copy link
Contributor

Yes I did. Seemingly, the recipe instantiates alright, but during the run phase it fails and I don't understand why. Wanted to double check it so I inserted some console prints into the recipe and they are printed as expected when the test is run. I will add some more simple matcher-to-method cases while I wait for the review.

Sorry about the delays in getting you feedback; It's been pretty busy with the new 8.0 release coming up.

I've posted two suggestions to get your first tests passing; That should help to start iterating by adding options, configuring those from Yaml, and see how far we can get with covering different Hamcrest matchers. Hope that helps!

Added translations for matchers closeTo, containsString, empty, emptyArray, emptyIterable, emptyCollectionOf, emptyIterableOf, endsWith.
Tests need to be added for each matcher.

Signed-off-by: matus.matok <[email protected]>
@matusmatokpt
Copy link
Contributor Author

No problem with the delays. Here is some further progress.

Some tests -- those that are supposed to add import of ..Assertions.assertTrue; do not add it, hence the tests fail.
I added a bunch of simple cases, they will need to have tests added, keeping that in mind.
For simplicity, I am adding all the simple cases into a single recipe. You keep mentioning something with YAML, but I think I will need more explanation in that department.
I will probably get to work on this in the next week, so the review is in no rush.
Thanks

@matusmatokpt matusmatokpt marked this pull request as ready for review August 3, 2023 09:22
timtebeek and others added 4 commits June 15, 2024 10:40
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@knutwannheden
Copy link
Contributor

I don't have the permission to push to the fork, but it looks like this patch would fix the compilation error. Maybe that is something you can apply, @timtebeek ?

Index: src/test/java/org/openrewrite/java/testing/hamcrest/RemoveNotMatcherTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/test/java/org/openrewrite/java/testing/hamcrest/RemoveNotMatcherTest.java b/src/test/java/org/openrewrite/java/testing/hamcrest/RemoveNotMatcherTest.java
--- a/src/test/java/org/openrewrite/java/testing/hamcrest/RemoveNotMatcherTest.java	(revision a18e7aceb1905fdd14a790899626ab2b875f19b3)
+++ b/src/test/java/org/openrewrite/java/testing/hamcrest/RemoveNotMatcherTest.java	(revision 0a4ef0be250926b82829d816d981d9d9e422dd0b)
@@ -34,8 +34,6 @@
     }
 
     @DocumentExample
-          java(
-                """
     void nestedNotMatcher() {
         rewriteRun(
           //language=java
@@ -44,7 +42,7 @@
             import static org.hamcrest.MatcherAssert.assertThat;
             import static org.hamcrest.Matchers.equalTo;
             import static org.hamcrest.Matchers.not;
-                            
+
             class ATest {
                 @Test
                 void testEquals() {
@@ -58,7 +56,7 @@
             import org.junit.jupiter.api.Test;
             import static org.hamcrest.MatcherAssert.assertThat;
             import static org.hamcrest.Matchers.equalTo;
-                            
+
             class ATest {
                 @Test
                 void testEquals() {
@@ -79,7 +77,7 @@
             import org.junit.jupiter.api.Test;
             import static org.hamcrest.MatcherAssert.assertThat;
             import static org.hamcrest.Matchers.not;
-                            
+
             class ATest {
                 @Test
                 void testEquals() {
@@ -93,7 +91,7 @@
             import org.junit.jupiter.api.Test;
             import static org.hamcrest.MatcherAssert.assertThat;
             import static org.hamcrest.Matchers.equalTo;
-                            
+
             class ATest {
                 @Test
                 void testEquals() {

timtebeek and others added 3 commits December 10, 2024 21:14
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

@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 for these recipes @matusmatokpt ; The review & polish took way longer than I had hoped, but I'm glad to get them in now. I look forward to folks using your recipes to move off of Hamcrest towards JUnit 5.

@timtebeek timtebeek merged commit f0d765a into openrewrite:main Dec 10, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants