You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We had a test able to call Mockito.mock from classes created in custom classloaders working in mockito 1.x, when upgrading to 3.x it fails.
The test code demonstrating the issue is:
public class MyTest {
public static class MyClass {
public static class MyOtherClass {
}
public void initialize() {
MyOtherClass mock = Mockito.mock(MyOtherClass.class);
}
}
@Test
public void myTest() throws Exception {
URLClassLoader customLoader = new URLClassLoader(getClasspathURLs(), null);
Class<?> clz = customLoader.loadClass(MyClass.class.getName());
Method mtd = clz.getMethod("initialize");
Object inst = clz.newInstance();
mtd.invoke(inst, new Object[0]);
}
private URL[] getClasspathURLs() {
return Arrays.stream(
System.getProperty("java.class.path").split(File.pathSeparator))
.map(s -> {
try {
return new File(s).toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList()).toArray(new URL[0]);
}
}
The exception I got is:
Caused by: org.mockito.exceptions.base.MockitoException:
ClassCastException occurred while creating the mockito mock :
class to mock : '...MyTest.MyClass.MyOtherClass', loaded by classloader : 'java.net.URLClassLoader@e73f9ac'
created class : 'org.mockito.codegen.MyOtherClass$MockitoMock$1564550697', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@75d4a5c2'
proxy instance class : 'org.mockito.codegen.MyOtherClass$MockitoMock$1564550697', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@75d4a5c2'
instance creation by : ObjenesisInstantiator
You might experience classloading issues, please ask the mockito mailing-list.
at ....MyTest$MyClass.initialize(MyTest.java:23)
... 31 more
Caused by: java.lang.ClassCastException: org.mockito.codegen.MyOtherClass$MockitoMock$1564550697 cannot be cast to org.mockito.internal.creation.bytebuddy.MockAccess
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:49)
The text was updated successfully, but these errors were encountered:
I believe this is actually a different failure; the MultipleParentsClassloader needs to be seeded with a set of existing classes (MockAccess, MyOtherClass, and any interfaces added to the mock), so that it knows which of the overlapping classloaders to use for MyOtherClass. Right now it's ordering dependent on what gets passed to MultipleParentsClassLoader.
@manolo Can you please check if you are still running into this issue on the latest version of Mockito? If yes, are you interested in contributing a fix? The relevant code is at
We had a test able to call
Mockito.mock
from classes created in custom classloaders working in mockito 1.x, when upgrading to 3.x it fails.The test code demonstrating the issue is:
The exception I got is:
The text was updated successfully, but these errors were encountered: