-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Mockito mock of interfaces lost annotation information #2640
Comments
4.5.0 included both #2614 and #2613 the other changes in the release (https://github.com/mockito/mockito/releases/tag/v4.5.0) don't appear to be interesting for a breakage here. @raphw Does this ring any bells for you? |
I'm not very familiar with the code, but i looked at the pull requests posted The change appears to be from #2613 These lines seems interesting to me. mockito/src/main/java/org/mockito/internal/creation/bytebuddy/SubclassBytecodeGenerator.java Lines 239 to 242 in d23dc0e
If the mock is an interface it will always stip annotations. Similar to the option stripAnnotations. Is this really intentional ? |
It wouldn't surprise me if we accidentally broke something here. @udengaardandersent-ELS do you mind working on a PR that includes your testcase as-is and modify the line you linked to make the test pass? (While keeping all the other tests as well of course) |
This was a deliberate change as we had some caching issues with the previous solution. We can only inherit the annotations of one superclass/supertype. Mockito would create the same mock for: mocked class: If you wanted to inherit annotations, if the second mock already was created and in the cache, the request for the first mock would lack annotations as the first call inherited them from mocked class: Alternatively, we could disable annotation inheritance only for the case with more then one annotation being involved and only inherit from a single annotation if the superclass was |
After upgrading to Mockito 4.5.1, the Spring @SpyBean annotation on Feign- and Resilience4J-annoyated REST client Java interfaces no longer works as it did with Mockito 4.4.x and earlier. More specifically, Resilience4J no longer registers its AOP proxy for the annotated interface which renders it unusable. This is on the latest Spring Boot 2.6.7. I think it is related to the problem described by the OP and I would appreciate if the behavior of Mockito 4.4.x for interface annotations could be reestablished on 4.5.x. If there are valid use cases for the new behavior, it could be hidden behind a feature flag and activated on demand. |
…nterface is mocked, including additional interfaces. Without this restriction, the first presented interface might determine the interfaces that are inherited by a subsequent mock that presents the interfaces in a different order. Also, it does not make semantic sense to decide on a particular interface to inherit annotations from. Fixes #2640.
I created a PR that fixes this issue for cases without additional interfaces. We can release a new version after its reviewed and merged. |
I see some different behaviour after updating mockito, and I think it's a bug.
See code below - the test fails because the mocked interface has lost the expected annotation.
Making mocks of classes, keeps the annotation information.
Making mocks of interfaces, annotation information is lost.
Junit version: junit:junit:4.13.2
mockito version: org.mockito:mockito-core:4.5.1
openJDK 11
The test works on previous versions of mockito like, 4.4.0
The text was updated successfully, but these errors were encountered: