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 Jmockit VerificationsInOrder to Mockito #632

Merged
merged 18 commits into from
Dec 11, 2024

Conversation

shivanisky
Copy link
Collaborator

@shivanisky shivanisky commented Nov 5, 2024

What's changed?

Add feature to migrate Jmockit VerificationsInOrder to mockito https://javadoc.io/doc/org.jmockit/jmockit/latest/mockit/VerificationsInOrder.html
https://www.javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/InOrder.html

What's your motivation?

Anyone you would like to review specifically?

@sambsnyd @timtebeek @tinder-dthomson as FYI

Any additional context

Only JMockit Expectations Delegate left to migrate.

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 marked this pull request as draft November 5, 2024 12:38
@shivanisky shivanisky self-assigned this Nov 5, 2024
@shivanisky shivanisky added the enhancement New feature or request label Nov 5, 2024
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.

Great to see you follow through with the penultimate feature needed to complete the JMockit migrations. I've done a very quick round of comments from GitHub; will need to look in more detail later.


class JMockitBlockRewriter {

private static final String WHEN_TEMPLATE_PREFIX = "when(#{any()}).";
private static final String VERIFY_TEMPLATE_PREFIX = "verify(#{any()}";
private static final String VERIFY_NO_INTERACTIONS_TEMPLATE_PREFIX = "verifyNoMoreInteractions(";
private static final String VERIFY_IN_ORDER_TEMPLATE_PREFIX = "inOrder(";
//private static final String VERIFY_IN_ORDER_TEMPLATE_PREFIX = "InOrder inOrder = inOrder(";
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
//private static final String VERIFY_IN_ORDER_TEMPLATE_PREFIX = "InOrder inOrder = inOrder(";

Copy link
Collaborator Author

@shivanisky shivanisky Nov 5, 2024

Choose a reason for hiding this comment

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

Ah yes - this is one issue I'd like to get input on
inOrder(mock) works, but
InOrder inOrder = inOrder(mock) causes error even though I have the right compilation options and the right import. Hence I am not able to get all the tests passing (have disabled for now).

Caused by: java.lang.IllegalArgumentException: Could not parse as Java
	at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$13(JavaTemplateParser.java:264)
	at java.base/java.util.Optional.orElseThrow(Optional.java:403)
	at org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:264)
	at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$parseBlockStatements$9(JavaTemplateParser.java:176)
	at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$cacheIfContextFree$14(JavaTemplateParser.java:287)
	at org.openrewrite.java.internal.template.JavaTemplateParser.cache(JavaTemplateParser.java:308)
	at org.openrewrite.java.internal.template.JavaTemplateParser.cacheIfContextFree(JavaTemplateParser.java:287)
	at org.openrewrite.java.internal.template.JavaTemplateParser.parseBlockStatements(JavaTemplateParser.java:171)
	at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.lambda$visitBlock$1(JavaTemplateJavaExtension.java:112)
	at org.openrewrite.internal.ListUtils.lambda$flatMap$0(ListUtils.java:261)
	at org.openrewrite.internal.ListUtils.flatMap(ListUtils.java:205)
	at org.openrewrite.internal.ListUtils.flatMap(ListUtils.java:261)
	at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock(JavaTemplateJavaExtension.java:110)
	at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock(JavaTemplateJavaExtension.java:55)
	at org.openrewrite.java.tree.J$Block.acceptJava(J.java:838)
	at org.openrewrite.java.tree.J.accept(J.java:59)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
	... 58 more

Copy link
Collaborator Author

@shivanisky shivanisky Nov 9, 2024

Choose a reason for hiding this comment

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

Ah all good, that parse error went away - thanks, maybe all that was needed was to sync with main! Now just only one test failing with some strange formatting issue, just an empy new line between statements in test whenThreeBlocks g. https://ge.openrewrite.org/s/pyyay4x3zaeui/tests/task/:test/details/org.openrewrite.java.testing.jmockit.JMockitVerificationsInOrderToMockitoTest/whenThreeBlocks()?top-execution=1

Not due to having 3 blocks, but due to having a very short InOrder block of 2 lines.

@timtebeek timtebeek added the recipe Recipe request label Nov 5, 2024
@timtebeek
Copy link
Contributor

Looks like we're very close, with only one failure left on whitespace

JMockitVerificationsInOrderToMockitoTest > whenThreeBlocks() FAILED
    org.opentest4j.AssertionFailedError: [Unexpected result in "MyTest.java":
diff --git a/MyTest.java b/MyTest.java
index 99fe3e8..bd0a50f 100644
--- a/MyTest.java
+++ b/MyTest.java
@@ -18,11 +18,15 @@ 
         inOrder.verify(obj).wait();

         obj.wait();
+
         InOrder inOrder1 = inOrder(obj);
+
         inOrder1.verify(obj).wait();

         obj.wait(10L, 10);
+
         InOrder inOrder2 = inOrder(obj);
+
         inOrder2.verify(obj).wait(anyLong(), anyInt());
     }
 }
\ No newline at end of file

expected:

import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class MyTest {
    @Mock
    Object obj;

    void test() {
        obj.wait(10L, 10);
        obj.wait();
        InOrder inOrder = inOrder(obj);
        inOrder.verify(obj).wait(anyLong(), anyInt());
        inOrder.verify(obj).wait();

        obj.wait();
        InOrder inOrder1 = inOrder(obj);
        inOrder1.verify(obj).wait();

        obj.wait(10L, 10);
        InOrder inOrder2 = inOrder(obj);
        inOrder2.verify(obj).wait(anyLong(), anyInt());
    }
}

but was:

import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class MyTest {
    @Mock
    Object obj;

    void test() {
        obj.wait(10L, 10);
        obj.wait();
        InOrder inOrder = inOrder(obj);
        inOrder.verify(obj).wait(anyLong(), anyInt());
        inOrder.verify(obj).wait();

        obj.wait();

        InOrder inOrder1 = inOrder(obj);

        inOrder1.verify(obj).wait();

        obj.wait(10L, 10);

        InOrder inOrder2 = inOrder(obj);

        inOrder2.verify(obj).wait(anyLong(), anyInt());
    }
}

@shivanisky
Copy link
Collaborator Author

shivanisky commented Dec 11, 2024 via email

@timtebeek
Copy link
Contributor

Ah check! Thanks for looking into it; also let me know if you'd like me to take over. The way I see it there's a few options:

  1. Counteract the left over whitespace in the recipe
  2. Try to resolve the issue upstream somewhere
  3. Adjust the tests to match the current output

All three could be a viable way to merge this PR, and don't necessarily rule out one another; I think folks would prefer to see these case migrated, even if there are excess newlines, for now. Then if the issue is fixed upstream we can revisit. Let me know what you find and which direction you want to go. :)

@shivanisky
Copy link
Collaborator Author

shivanisky commented Dec 11, 2024 via email

@timtebeek timtebeek marked this pull request as ready for review December 11, 2024 13:08
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 @shivanisky ! Great to see this verifications in order covered as well.

I explored the whitespace issue briefly, and noticed a missing semicolon, but that failed to resolve the issue. We'll accept that for now and can circle back later if needed. I don't think folks will stumble over this in the migration, so then it's better to see it covered.

@timtebeek timtebeek merged commit b10165d into main Dec 11, 2024
2 checks passed
@timtebeek timtebeek deleted the jmockit-verifications-in-order-mockito branch December 11, 2024 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request recipe Recipe request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants