当我尝试使用 Mockito 2(版本 2.0.54 测试版)在 eclipse 中运行 junit 插件测试时,调用 org.mockito.Mockito.mock(Class<T>, MockSettings)
失败,出现上述堆栈跟踪。
我设法通过这样做来解决问题
ClassLoader loader = Tread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(Mockito.class.getClassLoader());
try {
org.mockito.Mockito.mock(Class<T>, MockSettings);
} finally {
Thread.currentThread().setContextClassLoader(loader)
}
这似乎不对,并且需要大量工作才能从 mockito 1 移动到 mockito 2,有没有其他方法可以在 osgi 环境中使用 mockito?
java.lang.IllegalStateException: Error invoking java.lang.ClassLoader#findClass
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Resolved.loadClass(ClassInjector.java:401)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:210)
at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:204)
at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default.load(ClassLoadingStrategy.java:119)
at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4350)
at org.mockito.internal.creation.bytebuddy.MockBytecodeGenerator.generateMockClass(MockBytecodeGenerator.java:60)
at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.generate(CachingMockBytecodeGenerator.java:72)
at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.getOrGenerateMockClass(CachingMockBytecodeGenerator.java:64)
at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator.get(CachingMockBytecodeGenerator.java:27)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createProxyClass(ByteBuddyMockMaker.java:54)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:27)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:32)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:54)
at org.mockito.Mockito.mock(Mockito.java:1443)
at org.mockito.Mockito.mock(Mockito.java:1356)
... 45 more
Caused by: java.lang.NoClassDefFoundError: org/mockito/internal/creation/bytebuddy/MockMethodInterceptor$MockAccess
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Resolved.loadClass(ClassInjector.java:397)
... 59 more
Caused by: java.lang.ClassNotFoundException: org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$MockAccess
at java.lang.ClassLoader.findClass(ClassLoader.java:531)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at net.bytebuddy.dynamic.loading.MultipleParentClassLoader.loadClass(MultipleParentClassLoader.java:66)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 66 more
此问题在最近的 Mockito 版本中得到解决。