-
Notifications
You must be signed in to change notification settings - Fork 79
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
JMockit to Mockito #325
Comments
From my first research I found the following, that the recipe shall do:
import mockit.*; with import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
@Mocked
private MyClass myClassMock; with @Mock
private MyClass myClassMock;
For example, if you had a JMockit expectation like this: new Expectations() {{
myClassMock.myMethod(input);
result = output;
}}; You would refactor it to use Mockito like this: Mockito.when(myClassMock.myMethod(input)).thenReturn(output);
new Verifications() {{
myClassMock.myMethod(input);
times = 1;
}}; You would refactor it to use Mockito like this: Mockito.verify(myClassMock, Mockito.times(1)).myMethod(input); @nmck257 Is there anything else, that is missing from your point of view? |
Honestly, I've never used JMockit myself, but that looks like an excellent scope to start with. :) Note that RunWith is JUnit4 syntax, so we may want to be mindful about how this recipe should interact with the JUnit 5 migration recipe(s) |
This would be great! I'm currently maintaining jmockit and have been for few years now. Only to keep it alive and working on newer jdks. The original author blocked me, went MIA, was not very OSS friendly at all. I have most if not all PRs people have raised into my fork, its identical otherwise. But having ability to rewrite it back to mockito would be best option. In my company we were forced to move to jmockit in 2015 with claims mockito didn't do this or that which were 100% wrong. But again we were forced. Now with it dead and I have it on life support, its only going to work not get new features and the code base is really java 7 based mostly. Its javaEE based too but I'll get it to jakartaEE. Usage wise though from a rewrite, it should not change how it looks and having gone from mockito all repo with 90% coverage in 2015 entirely to jmockit, going backwards should not be that hard. There are certainly items to think more about such as Deencapsulation as many stayed on old versions where that was removed. The natural replacement at the time was Powermock.Whitebox. But I don't know what that should be today as powermock is also dead. I can try to help out but don't have a lot of time to do so. But wanted to chime in as this is a good suggestion. |
The supported copy of the repo is here https://github.com/hazendaz/jmockit1. It is the only one that properly works with jacoco and jdk11+. It should work through jdk 20 currently for reference. The master there is broken on GHA for the moment but will fix in coming week or so. |
Sorry and finally you can find the releases to show its been done by me since 2020 -> https://search.maven.org/artifact/com.github.hazendaz.jmockit/jmockit |
Figured update this issue that we've since had a steady stream of work in migration recipes from JMockit to Mockito, mostly from @tinder-dthomson 🙏🏻
There's already a first recipe available for folks to use: Curious to know what else we would need to cover before we can close this issue. Typically it's hard to say at what point we're done with an issue like this. We can also opt to close this issue and open up new issues for specific extensions if that makes more sense. |
@timtebeek Hey there! My recommendation is we use this issue as a tracker for the remaining items to resolve. I have one set of changes (locally) that improves handling of parameterized class arguments like I'm am aware, however, of these big ticket items: - There are also many JMockit features that we haven't even mentioned. |
Hi there, This is a good list to work from. The static method mock is a big one and I've seen a few different ways that this is set up using JMockit. 1. Class passed to expectation
Passing a Class in the Expectations constructor has been removed since JMockit 1.48 2. Mocked method parameter/field
What would a Mockito equivalent look like?The Mockito equivalent might look something like this
Mockito docs suggest keeping the mock in the scope of a try-with-resources block but how would we determine what should be in scope? If the mock is defined as a method parameter like above, do we avoid the try-with-resources, create the MockedStatic at the start of the test and close at the end of the test? If a @mocked field is defined, do we create the MockedStatic in the @beforeeach and close in the @AfterEach? |
@kernanj I've thought about this quite a bit. The best approach that works with the existing recipe I've come up with looks like this:
Using your example above, the recipe processing would go like this:
This would re-use the heavy lifting of the Finally, regarding wrapping the entire test method using |
@tinder-dthomson We would need to put the remainder of the test in scope of the try with resources though. I've given an example below where there are multiple static methods being mocked in an expectation block.
|
"All the remaining statements of the test method body, including the current Expectations statement, should be written as the try-with-resources block body" missed this point you made above, I think we're on the same page here 🙂 |
You can use this Jmockit fork https://github.com/hazendaz/jmockit1/ It's maintained and supports Java 11+ |
hi @tinder-dthomson I have created a JMockitMockUpToMockito recipe and want to contribute, what should I do? Just create a PR? |
With #599 merged just now I think we've covered a good deal op JMockit features already. I propose we close this overarching issue and open new separate issues for additional features folks might like to cover. Thanks all for the work done here! It's been great seeing this come together with so much community contributions. |
JMockit remains popular, but has not had a release since 2019, and has various compatibility issues with Java 11+.
As Java 8 loses LTS status, developers would likely benefit from some recipe(s) to facilitate migration from JMockit to Mockito
The text was updated successfully, but these errors were encountered: