Open GoogleCodeExporter opened 8 years ago
I ran the code on-device with the latest Mockito and it worked just fine.
One potential issue: this may prevent us from mocking package-private methods.
I don't think this is a big deal 'cause we're already broken for
package-private methods - we don't generate code in the correct package;
everything just goes in the default package.
According to the Mockito source, they aggregate multiple class loaders to do
their dirty work. Mockito combines the mocked type's class loader, Mockito's
class loader, and the context class loader. That's a bit nasty!
https://github.com/mockito/mockito/blob/master/src/org/mockito/internal/creation
/jmock/SearchingClassLoader.java
I think their extra class loader guarantees that they won't work with
package-private methods due to the same classloader rule. They have a unit test
that suggests otherwise, MockingPackageProtectedTest.java. I wonder whether
they support mocking package-private methods? It's definitely broken in OSGi
but I'm surprised it works elsewhere.
https://code.google.com/p/mockito/issues/detail?id=127
Next steps: decide whether we should combine class loaders. We could probably
do it in a few lines of code:
public ClassLoader combine(final ClassLoader... loaders) {
return new ClassLoader() {
@Override protected Class<?> findClass(String name) throws ClassNotFoundException {
for (int i = 0; i < loaders.length - 1; i++) {
try {
return loaders[i].loadClass(name);
} catch (ClassNotFoundException ignored) {
}
}
return loaders[loaders.length - 1].loadClass(name); // Don't ignore failures from the last loader!
}
};
}
Original comment by limpbizkit
on 9 Feb 2013 at 3:00
That's not a bit nasty, that's a lot nasty!
My understanding of ClassLoaders is fairly rusty and I'm not quite sure I've
got my head wrapped around this package-private method problem.
My patch only applies to mocking interfaces, not concrete classes, I think?
Playing with a little toy code, it looks to me like using the context
ClassLoader still allows us to proxy package-private interfaces (so long as
nobody's mucked with the context ClassLoader too much).
A delegating ClassLoader like yours does seem fail to create a package-private
proxy, as does a chaining ClassLoader that sets the ClassLoaders up as parents
of each other (like Mockito's).
Original comment by i...@google.com
on 11 Feb 2013 at 1:11
https://groups.google.com/forum/?fromgroups=#!topic/mockito/pmAzNAnVOmE
Original comment by limpbizkit
on 26 Feb 2013 at 12:08
Waiting on our Mockito friends to decide if they're fixing this or we are.
Original comment by limpbizkit
on 2 Mar 2013 at 2:43
Original issue reported on code.google.com by
i...@google.com
on 6 Feb 2013 at 1:59Attachments: