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
NullPointerException in java.lang.reflect.Method.getParameterTypes #2026
Comments
We are going to need a small reproduction case for us to debug this problem. |
Please create a small reproduction case, e.g. a GitHub project that we can run and debug. |
I'll try to do one next week, after I return from vacation, but, in the meantime, I've commented out tests one-by-one, until I discovered what triggers the problem.... One of them has a Removing the mocking of the |
I've managed to make a reproducer 🙂 |
Is it ok like this? |
I could confirm that have this issue too. We have in one class
When this class that contains this mocking is run alone that the test is green in Eclipse and maven too. When it run with other test all following test failed on this NPE. For me it looks like that after the test with mocked Final class isn't correctly released and next tests instead of call of original java.lang.reflect.Method type calls it on mock. |
A temporary workaround is to fork each test, though build times suffer somewhat. <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
</configuration>
</plugin> |
Also ran into this issue when upgrading from Mockito 3.3.3 to Mockito 3.6.0 (from spring-boot 2.3.6 to 2.4.0). It seems that creating a mock of a particular class/interface in a lot of tests from independent test classes exposes this problem. In my case I am mocking an interface, final is not involved. @andrei-ivanov 's workaround works, but is quite slow. |
Mocking of classes in Therefore, I would be inclined to close this issue as WontFix, unless @nguyenquoc you are mocking a non- |
Any chance to get an error or a warning for these classes if we try to mock them? |
Yes I have worked on a prototype to make that happen: #1833 Sadly I haven't had the time to get back to that. |
As we solved it by rewrite unfinished feature. We don't need mock this class now. Maybe would be nice solve it in same way as is e.g. for String class so some error message that point to this. From current documentation it isn't really clear if this is or isn't supported. There is mentioned that some of aren't supported. |
This is documented in our wiki: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own Our intention with |
I read the #1833 and sounds as good improvement. Problem with wiki is that you read it probably when you start with Mockito as newer. We are using in our project mockito long time. We started with 1.10.19 version. So we mostly read now only what is new = e.g. Mockito class javadoc where is enumeration of features. So I mean the point 39 where is about mocking final (new feature for us that allow leave Powermock in combination of static mock). And here is not fully clear. Especially when for String you got error but for java.lang.reflect.* no. I wasn't author of our test. Just find what was problem. Original implementation is changed in the meantime and we don't mock java.lang.reflect.* now. |
@TimvdLippe : In my case I am mocking the spring-beans org.springframework.beans.factory.BeanFactory interface:
If I run the test class by itself, then the mock is successful. If I run the test class in a suite of tests (e.g. mvn install on a whole product), then the mock is created, but is defective, e.g. toString() results in a null pointer exception, and using the mock also results in a null pointer exception. My tests work with Mockito 3.3.3, but fail with 3.6.0. |
@TimvdLippe In my case I'm mocking also my own classes and get the same NPE. What can we do to help you to solve this issue ? I don't know if it helps but in Java 15 I got this log :
|
Sadly I don't have a lot of free cycles to investigate this issue. What would help is to perform a bisect of Mockito versions to figure out which specific PR is causing issues. You can use our Bintray repository which hosts all of our versions to hopefully determine the exact version at which things start to break: https://bintray.com/mockito/maven/mockito |
I tried different version. And it is the 3.5.0 version that made the break for me. When I pass from mockito(-jupiter) 3.4.6 to 3.5.0 everything goes wrong. There are not so much diff : v3.4.6...v3.5.0, but for me simple (humble, grateful ;-)) user, it is Klingon ! |
Mocking As for the error occuring when not mocking core classes. Did you try to remove mocks until the issue no longer occurs? This can help to break it down to see which class would cause this. I'd need more detail to to look into it. |
I finally reached the issue. I was mocking Method too, but not in any test in error, not so easy to find, I have more than 300 in this project. That's means I also used the feature to mock final object |
Hi everyone, I have exactly this issue for existing tests migrating to any mockito from 3.5.0 to the current latest 3.10.0 |
I removed the mocking of |
Yes, indeed, now I found one mock of the Method which indeed the culprit. So for everyone else landing on this page in the future, try your best to find in your code mocking of java.lang.reflect.Method and address those tests. |
This took me three days of searching... Thanks for the help and suggestions! |
Unfortunately, |
## 🎫 Ticket https://jira.cms.gov/browse/DPC-3494 ## 🛠 Changes Updated Mockito from 3.4.6 to 5.4.0. Removed mocking of Method class from FHIRRequestFeatureTest. Updated BaseResourceUnitTest to use new MockedStatic and MockedConstruction. ## ℹ️ Context for reviewers We were running an old version of Mockito that didn't support mocking static or final methods, or constructors, and this was preventing us from pushing code coverage as far as we otherwise could. The new version of Mockito was also causing unit tests to fail randomly and erratically. The new version was causing tests to fail because an old test in `FHIRRequestFeatureTest` was mocking the `Method` class from `java.lang.reflect`. Mockito depends on this class, along with a few others from that package, and mocking them is essentially rewriting Mockito's plumbing while it's running. This leads to any tests running afterwards potentially failing in random ways. See issue [here](mockito/mockito#2026 (comment)) for a better explanation. ## ✅ Acceptance Validation We're running the new version of Mockito, we're using some of its new features, and all unit tests are passing. ## 🔒 Security Implications - [ ] This PR adds a new software dependency or dependencies. - [ ] This PR modifies or invalidates one or more of our security controls. - [ ] This PR stores or transmits data that was not stored or transmitted before. - [ ] This PR requires additional review of its security implications for other reasons. If any security implications apply, add Jason Ashbaugh (GitHub username: StewGoin) as a reviewer and do not merge this PR without his approval.
Since mocking method causes an issue one approach to avoid it is to use reflection to get the real method instead. e.g.
|
@TimvdLippe: While I understand concerns with mocking Would you be open to a PR that adds this as an enforcement to the
For others that may benefit, the above file has to be registered as a plugin by creating:
|
We should indeed add this to Can you send us a PR that updates |
Since I am only aware of |
I think |
Any type in |
I've encountered the very same problem, and it was caused by a conflict where the same class was mocked in different tests. When these tests ran simultaneously (in the same JVM), Mockito would randomly throw a NPE. Using @junkdog's solution fixed the problem (force Maven's surefire plugin to not use the same JVM for the different tests). I just want to highlight that this problem is not caused only by mocking |
Trying to upgrade Mockito from
3.4.6
(3.4.8
wasn't published to Maven central) to anything3.5.x
(3.5.7
included) and I get some weird NPEs, which don't happen if I run each test on its own:Initially I thought it might be caused by the new
MockedStatic
usage, but I've marked those classes with@Disable
and the exceptions happen anyway and the test classes that are affected weren't usingMockedStatic
anyway, so I'm not exactly sure how to investigate this further.The text was updated successfully, but these errors were encountered: