Cannot cast org.powermock.api.mockito.mockmaker.PowerMockMaker to org.mockito.plugins.MockMaker

1,696 views
Skip to first unread message

cheungmi...@outlook.com

unread,
Apr 11, 2018, 2:30:25 PM4/11/18
to PowerMock
Hi I'm getting this error and am a bit lost with what I have done wrong.  PowerMockMaker does implement MockMaker so this should work but running a debug session does show it's using the Class.cast method and at the line mentioned on the stack trace it turns out PowerMockMaker is not an instance of MockMaker so is failing.  Not sure where to look really.

Any ideas?  The code is pasted below.

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
 at org
.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
 at com
.sun.proxy.$Proxy48.getHandler(Unknown Source)
 at org
.mockito.internal.util.MockUtil.isMock(MockUtil.java:81)
 at org
.mockito.internal.configuration.injection.scanner.MockScanner.isMockOrSpy(MockScanner.java:83)
 at org
.mockito.internal.configuration.injection.scanner.MockScanner.preparedMock(MockScanner.java:71)
 at org
.mockito.internal.configuration.injection.scanner.MockScanner.scan(MockScanner.java:59)
 at org
.mockito.internal.configuration.injection.scanner.MockScanner.addPreparedMocks(MockScanner.java:45)
 at org
.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:82)
 at org
.powermock.api.mockito.internal.configuration.PowerMockitoInjectingAnnotationEngine.process(PowerMockitoInjectingAnnotationEngine.java:36)
 at org
.powermock.api.extension.listener.AnnotationEnabler.injectSpiesAndInjectToSetters(AnnotationEnabler.java:61)
 at org
.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:56)
 at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java
.lang.reflect.Method.invoke(Method.java:498)
 at org
.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1846)
 at org
.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:700)
 at org
.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415)
 at org
.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.injectMocksUsingAnnotationEnabler(PowerMockTestNGMethodHandler.java:76)
 at org
.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:48)
 at com
.QuickTest_$$_jvst17e_0.springTestContextBeforeTestClass(QuickTest_$$_jvst17e_0.java)
 at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java
.lang.reflect.Method.invoke(Method.java:498)
 at org
.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
 at org
.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
 at org
.testng.internal.Invoker.invokeConfigurations(Invoker.java:216)
 at org
.testng.internal.Invoker.invokeConfigurations(Invoker.java:143)
 at org
.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:169)
 at org
.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
 at org
.testng.TestRunner.privateRun(TestRunner.java:753)
 at org
.testng.TestRunner.run(TestRunner.java:607)
 at org
.testng.SuiteRunner.runTest(SuiteRunner.java:368)
 at org
.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
 at org
.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
 at org
.testng.SuiteRunner.run(SuiteRunner.java:270)
 at org
.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
 at org
.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
 at org
.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
 at org
.testng.TestNG.runSuitesLocally(TestNG.java:1209)
 at org
.testng.TestNG.runSuites(TestNG.java:1124)
 at org
.testng.TestNG.run(TestNG.java:1096)
 at org
.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
 at org
.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@2f1de2d6
 at org
.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:54)
 at org
.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:57)
 at org
.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:44)
 at org
.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:21)
 at org
.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:18)
 at org
.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
 
... 42 more
Caused by: java.lang.ClassCastException: Cannot cast org.powermock.api.mockito.mockmaker.PowerMockMaker to org.mockito.plugins.MockMaker
 at java
.lang.Class.cast(Class.java:3369)
 at org
.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:50)
 
... 47 more


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.IObjectFactory;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;
 
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
 
@ActiveProfiles("QuickTest")
@Component
@ContextConfiguration(classes = AppConfigQuickTest.class)
@PrepareForTest({SocketChannel.class, QuickTest.class})
@PowerMockIgnore({"org.apache.logging.log4j.*"})
public class QuickTest extends AbstractTestNGSpringContextTests {

 
private static final Logger logger = LogManager.getLogger("QuickTest");
 
 
@ObjectFactory
 
public IObjectFactory getObjectFactory() {
 
return new org.powermock.modules.testng.PowerMockObjectFactory();
 
}
 
 
@Test
 
public void socketTest() throws IOException {
 
int bytesWritten = 5;
 
SocketChannel socketChannel = PowerMockito.mock(SocketChannel.class);
 
SocketAddress socketAddress = new InetSocketAddress("abc.com", 12340);
 
Mockito.when(socketChannel.getRemoteAddress()).thenReturn(socketAddress);
 
Mockito.when(socketChannel.write((ByteBuffer) Mockito.any())).thenReturn(bytesWritten);
 
// todo: socketChannel.register() is causing Mockito to throw null pointer exception because it's a final method
 
Mockito.doNothing().when(socketChannel.register(Mockito.any(), Mockito.any()));
 
 
// further stuff down here to setup and do test
 
}
}

Zagretdinov Arthur

unread,
Apr 13, 2018, 5:38:19 AM4/13/18
to powe...@googlegroups.com
Hi, 

PowerMockMaker and MockMaker are loaded by different class loaders. It is a know issue with TestNG and its @ObjectFactory method. Try to add “org.mockito.plugins” to @PowerMockIgnore. 

Best Regards,
Arthur Zagretdinov


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at https://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

cheungmi...@outlook.com

unread,
Apr 14, 2018, 2:48:21 AM4/14/18
to PowerMock
Thanks.  Just tried adding that to the ignore annotation but am getting the following error now.  I'm surprised I haven't found the issue you mentioned because I was digging online quite a bit.  Can you tell me where was this reported or how did you find this out?

14:40:57.177 [org.springframework.test.context.TestContextManager:246] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4e9658b5] to prepare test instance [com.QuickTest_$$_jvst76a_0@16b2bb0c]

java
.lang.IllegalStateException: Failed to load ApplicationContext

 at org
.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
 at org
.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107)
 at org
.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
 at org
.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
 at org
.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)
 at org
.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:145)
 at com
.QuickTest_$$_jvst76a_0._d15springTestContextPrepareTestInstance(QuickTest_$$_jvst76a_0.java)

 at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java
.lang.reflect.Method.invoke(Method.java:498)

 at org
.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:50)
 at com
.QuickTest_$$_jvst76a_0.springTestContextPrepareTestInstance(QuickTest_$$_jvst76a_0.java)

 at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java
.lang.reflect.Method.invoke(Method.java:498)
 at org
.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
 at org
.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
 at org
.testng.internal.Invoker.invokeConfigurations(Invoker.java:216)
 at org
.testng.internal.Invoker.invokeConfigurations(Invoker.java:143)
 at org
.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:169)
 at org
.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
 at org
.testng.TestRunner.privateRun(TestRunner.java:753)
 at org
.testng.TestRunner.run(TestRunner.java:607)
 at org
.testng.SuiteRunner.runTest(SuiteRunner.java:368)
 at org
.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
 at org
.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
 at org
.testng.SuiteRunner.run(SuiteRunner.java:270)
 at org
.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
 at org
.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
 at org
.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
 at org
.testng.TestNG.runSuitesLocally(TestNG.java:1209)
 at org
.testng.TestNG.runSuites(TestNG.java:1124)
 at org
.testng.TestNG.run(TestNG.java:1096)
 at org
.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
 at org
.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
Caused by: java.lang.IllegalStateException: Cannot load configuration class: com.AppConfigQuickTest
 at org
.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:416)
 at org
.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:254)
 at org
.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
 at org
.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:128)
 at org
.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:693)
 at org
.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
 at org
.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
 at org
.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
 at org
.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:107)
 at org
.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:243)
 at org
.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
 at org
.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
 
... 36 more
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.NoSuchMethodException-->com.AppConfigQuickTest$$EnhancerBySpringCGLIB$$f4ca14f0.CGLIB$SET_THREAD_CALLBACKS([Lorg.springframework.cglib.proxy.Callback;)
 at org
.springframework.cglib.proxy.Enhancer$EnhancerFactoryData.<init>(Enhancer.java:423)
 at org
.springframework.cglib.proxy.Enhancer.wrapCachedClass(Enhancer.java:709)
 at org
.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:94)
 at org
.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
 at org
.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
 at java
.util.concurrent.FutureTask.run(FutureTask.java:266)
 at org
.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
 at org
.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
 at org
.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
 at org
.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
 at org
.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
 at org
.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337)
 at org
.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:138)
 at org
.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:110)
 at org
.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:405)
 
... 47 more
Caused by: java.lang.NoSuchMethodException: com.AppConfigQuickTest$$EnhancerBySpringCGLIB$$f4ca14f0.CGLIB$SET_THREAD_CALLBACKS([Lorg.springframework.cglib.proxy.Callback;)
 at java
.lang.Class.getDeclaredMethod(Class.java:2130)
 at org
.springframework.cglib.proxy.Enhancer.getCallbacksSetter(Enhancer.java:809)
 at org
.springframework.cglib.proxy.Enhancer.access$000(Enhancer.java:63)
 at org
.springframework.cglib.proxy.Enhancer$EnhancerFactoryData.<init>(Enhancer.java:414)
 
... 61 more

Habeeb Rahman Abdul Khadar

unread,
Jun 7, 2018, 2:07:39 AM6/7/18
to PowerMock
Was there any any workaround for this issue? I am also facing the same. I use PowerMock 1.7.1, Mockito-core 2.8.9, TestNG 6.10
Reply all
Reply to author
Forward
0 new messages