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

Restore previously deleted methods associated with MockUp classes. #158

Merged
merged 6 commits into from
Dec 13, 2023

Conversation

yukkes
Copy link

@yukkes yukkes commented Dec 1, 2023

Thanks you for your excellent work, I have restored previously deleted methods associated with MockUp classes.
I have attempted to fully restore the processing of past versions of JMockit, but have had some setbacks.
I hope you recognize this as an area for future improvement.

  • The number of invocations is ignored because there is no judgment process such as @mock(invocations = 1).

  • The following errors have been resolved. (12/12/2023)

    • The javadoc generation fails because ConstructorReflection refers to "sun.reflect.*" of a private package in Java 9 or later. The error has been avoided by adding "--ignore-source-errors".
    • The test fails because only the last value set in the following test case can be retrieved.
       @Test
       public void multiplePublicMockUps(){
          AClass mock1 = new AClassMockUp("Abc").getMockInstance();
          AClass mock2 = new AClassMockUp("Xpto").getMockInstance();
    
          assertNotSame(mock1, mock2);
          assertEquals("Abc", mock1.getTextValue());
          assertEquals("Xpto", mock2.getTextValue());
          assertTrue(AClass.doSomething());
       }

@hazendaz
Copy link
Owner

hazendaz commented Dec 2, 2023

Hi @yukkes, yes very much appreciate this. The original author I could never understand why he consistently deleted support making upgrades next to impossible. So I get everything is a work in progress and welcome all your efforts here to restore any needed features that will make your own migrations and others much easier. I think we all know this library is just maintenance but it needs to work and easily and hopefully up through higher jdks than current as the cost of migrating is high. I do know open rewrite is working on trying to have a migration path to mockito and are very well aware of this fork. I think their hurdles have been much of the lost support which makes it hard for them to have the latest here and somehow upgrade the older copies. So I think this will greatly help so keep it coming :)

@hazendaz
Copy link
Owner

hazendaz commented Dec 2, 2023

As noted on the other PR, I think we need both 11 and 17 working here before we merge. 21 is going to fail. I can just remove it from the builds when I get some time as its an ongoing issue. But the master currently work son 11 and 17.

@hazendaz
Copy link
Owner

hazendaz commented Dec 2, 2023

For the sun.reflect, can you write that any differently so it might work on 17? Presume that was when it was either entirely blocked or maybe removed, not sure. Would need to look. Its ok if the code doesn't come back exactly like it was. The main thing is that it just works for the folks that need it.

@hazendaz hazendaz self-assigned this Dec 2, 2023
@hazendaz
Copy link
Owner

hazendaz commented Dec 2, 2023

holding off this one for now since jdk 17 failed. Please look into that a bit more.

@yukkes
Copy link
Author

yukkes commented Dec 2, 2023

Thanks for the code review. I'll check back later as I'm spending time with my family right now.

@yukkes
Copy link
Author

yukkes commented Dec 2, 2023

Fixed not to reference the sun.reflect package.
The following blog in Japanese was used as a reference.
https://matsudamper.hatenablog.com/entry/2020/05/17/103231

    public static <T> T newUninitializedInstance(@Nonnull Class<T> aClass) {
        SunReflectionFactoryInstantiator<T> ref = new SunReflectionFactoryInstantiator<>(aClass);
        return ref.newInstance();
    }

@yukkes
Copy link
Author

yukkes commented Dec 2, 2023

@hazendaz, thanks for the accurate review remarks.
We have been using JMockit 1.20 for our internal system development and the latest version (1.49) has removed existing functionality, making it difficult to upgrade.
We have now decided to update from Java 8 to 17, and the large amount of test code has made it difficult to migrate to anything but JMockit. So I made JMockit work without modifying the existing test code.

I committed to this project because we plan to continue using JMockit after Java 21. I hope others will find it useful in the future.

@yukkes
Copy link
Author

yukkes commented Dec 2, 2023

The reason for the test failure in Java 21 is that the class to be mocked is now a sealed class.
The only solution seems to be to ignore the test.

  • JDK18 or earlier
public class InetAddress implements java.io.Serializable {
  • JDK19 or later
public sealed class InetAddress implements Serializable permits Inet4Address, Inet6Address {

@hazendaz
Copy link
Owner

hazendaz commented Dec 2, 2023

The reason for the test failure in Java 21 is that the class to be mocked is now a sealed class. The only solution seems to be to ignore the test.

* JDK18 or  earlier
public class InetAddress implements java.io.Serializable {
* JDK19 or later
public sealed class InetAddress implements Serializable permits Inet4Address, Inet6Address {

Agreed, I started looking at migrating most of the tests to junit 5 only using open rewrite but it had a very hard issue here. So my plan is to do that then simply disable for higher jdk. Thank you for finding the info. Do note this did not prevent this from working on java 21. I use it at work without issues already at that level. But for our audience faster we fix to show it works on each the more contributors may want to engage and feel more comfortable its working.

main/pom.xml Outdated
@@ -9,7 +9,6 @@
<version>1.51.1-SNAPSHOT</version>
</parent>

<groupId>com.github.hazendaz.jmockit</groupId>
Copy link
Owner

Choose a reason for hiding this comment

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

I'll add this back, its a strong preference of mine to be strict in poms of the GAV. I know m2e in eclipse is all messed up since it has warnings even if set to not warn in recent releases. Won't bother you with that here, I'll take care of no issues.

throw new IllegalArgumentException(
"Unsupported fake for private " + kindOfMember + privateMemberDesc + " found");
}
// Supported fake for private method
Copy link
Owner

Choose a reason for hiding this comment

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

@yukkes Can you add a simple further note here that we are keeping commented out code from original changes? Normally I'd say delete it but I think in this case I agree commenting it out is fine but want to make sure its obvious why we are keeping commented out code, otherwise someone will delete it it in the future.

thrown.expectMessage("Component");
thrown.expectMessage("checkCoalescing()");
thrown.expectMessage("found");
// Enabled to fake a private constructor.
Copy link
Owner

Choose a reason for hiding this comment

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

Same on these, just a bit more clarlity as to why we are keeping commented out code.

@hazendaz
Copy link
Owner

hazendaz commented Dec 2, 2023

@yukkes Very minor asks to make it a bit more clear why intention to keep commented out code. You may want to date it as well I typically will add a todo as well so its easily seen, something like this with XXX (your initials)

// TODO 12/2/2023 XXX Reason why we are doing something odd

@yukkes yukkes force-pushed the develop-restore-mockup-classes branch from 10d7e67 to 8bbc18a Compare December 3, 2023 02:44
@yukkes
Copy link
Author

yukkes commented Dec 3, 2023

@hazendaz , thanks for the code review. I fixed the code.
I had included the company email address in last night's commit, so I overwrote it with force-push. Sorry it's hard to see the commit history.

@yukkes
Copy link
Author

yukkes commented Dec 9, 2023

Please wait for the merge as we are in the process of resolving JMockit-related errors in our internal project.
It is expected to take about a week.

@hazendaz
Copy link
Owner

hazendaz commented Dec 9, 2023 via email

@yukkes
Copy link
Author

yukkes commented Dec 11, 2023

I have restored MockUp class and test class based on JMockit ver. 1.35, please review and merge if there is no problem.

The following test class is restored and all tests PASS.

  • MockInvocationProceedTest.java
  • MockInvocationTest.java
  • MockLoginContextTest.java
  • MockUpForGenericsTest.java
  • MockUpForSingleClassInstanceTest.java
  • MockUpForSingleInterfaceInstanceTest.java

However, the MockUpForSingleInterfaceInstanceTest failed the test as is, so I commented it out with a TODO comment. The reason for this is that the FieldInjection class has changed and no longer sets default values.

@hazendaz hazendaz merged commit a7bfa92 into hazendaz:master Dec 13, 2023
6 of 9 checks passed
@hazendaz
Copy link
Owner

@yukkes Are you good at this point for me to push a release?

@yukkes
Copy link
Author

yukkes commented Dec 13, 2023

@hazendaz Yes, please release.
The changes include restoring the MockUp and Deencapsulation classes based on JMockit ver. 1.35.

Note that the annotation to determine the number of method invocations has been restored as shown below, but since there is no judgment process, there is no error even if the number of invocations is different.
@Mock(invocations=1)

@hazendaz
Copy link
Owner

Release is out now. Let me know how that works out for you.

@yukkes
Copy link
Author

yukkes commented Dec 15, 2023

@hazendaz Thank you for the release.
I will check if the unit test passes using the latest version on our own project.

@yukkes
Copy link
Author

yukkes commented Dec 16, 2023

@hazendaz
I have performed my own unit tests and the results match my own build and ver. 1.52.0, so it looks fine. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants