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

ClassLoader built by mockito is not able to load declared types in the hierarchy in OSGi #1584

Closed
nfalco79 opened this issue Jan 9, 2019 · 1 comment

Comments

@nfalco79
Copy link
Contributor

nfalco79 commented Jan 9, 2019

We develop in OSGi environment and we are using latest version of mockito available on maven central 2.23.4

We got an issue when we mock classes given to guice (sisu) for injection. Sisu/Guice analyse all declared fields and methods for the input type and its super classes to built a sort of index of known types. In OSGi it fails with ClassNotFoundException on mocked classes. The reason is that the mocked class built with SubclassBytecodeGenerator has a classloader not able to load types reachable only by supertypes. In OSGi this does not happen because each class has associated the classloader from which is loaded. This guarantee that the class is able to reach/load each type it declares (by its methods, fields or annotations).

Example:

  • Bundle A
    foo.Class1 has a protected method that returns foo.impl.OtherClass
    foo.impl.OtherClass

  • Bundle B (imports only foo package from bundle A)
    acme.Class2 extends Class1

Mockito.mock(foo.Class1).getDeclaredMethods() will fails. To fix this issue SubclassBytecodeGenerator should compose a classloader (MultipleParentClassLoader) with also the classloader of superclass the mocked type (features.mockedType).

@ChristianSchwarz
Copy link
Contributor

@raphw FYI

SamBarker pushed a commit to SamBarker/mockito that referenced this issue Feb 25, 2019
…aders of supertypes of the mockedType so in OSGi is able to load all declared types in the hierarchy (mockito#1585)
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

No branches or pull requests

2 participants