Exception while using PowerMock with Mockito and testNG

515 views
Skip to first unread message

Habeeb Rahman Abdul Khadar

unread,
Jun 7, 2018, 2:07:39 AM6/7/18
to PowerMock

I am writing the test cases using PowerMock with Mockito and TestNG
PowerMock version 1.7.1
Mockito-core 2.8.9
TestNG 6.10

Here is the code snippet:


@PrepareForTest({SPJUtil.class})
public class SPJUtilTest{

    @Mock
    private Vendor vendor;

    @Test
    public void getVendorConfigJSONTestNull() throws Exception{
        spy(SPJUtil.class);
        MarketConfigurationServiceImpl mock = mock(MarketConfigurationServiceImpl.class);
        whenNew(MarketConfigurationServiceImpl.class).withNoArguments().thenReturn(mock);
        when(mock.getAllVendorConfigurationsForUser(vendor)).thenReturn(null);
        String vendorConfig = SDKUtil.getVendorConfigJSON(vendor);
        Assert.assertEquals(vendorConfig, "");
    }

    @ObjectFactory
    public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
    }
}

Getting the following exception while running the test case

FAILED: getVendorConfigJSONTestNull

   [testng] org.mockito.exceptions.base.MockitoException: 

   [testng] ClassCastException occurred while creating the mockito mock :

   [testng]   class to mock : 'com.saba.marketplace.entity.Vendor', loaded by classloader : 'org.powermock.core.classloader.MockClassLoader@64a40280'

   [testng]   created class : 'com.saba.marketplace.entity.Vendor$MockitoMock$1143590419', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@36c783ca'

   [testng]   proxy instance class : 'com.saba.marketplace.entity.Vendor$MockitoMock$1143590419', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@36c783ca'

   [testng]   instance creation by : ObjenesisInstantiator

   [testng] 

   [testng] You might experience classloading issues, please ask the mockito mailing-list.

   [testng] 

   [testng] at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:107)

   [testng] at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:55)

   [testng] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   [testng] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

   [testng] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

   [testng] at java.lang.reflect.Method.invoke(Method.java:483)

   [testng] at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1846)

   [testng] at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:700)

   [testng] at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415)

   [testng] at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.injectMocksUsingAnnotationEnabler(PowerMockTestNGMethodHandler.java:76)

   [testng] at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:48)

   [testng] at com.saba.sdk.util.SDKUtilTest_$$_jvst838_0.getVendorConfigJSONTestNull(SDKUtilTest_$$_jvst838_0.java)

   [testng] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   [testng] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

   [testng] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

   [testng] at java.lang.reflect.Method.invoke(Method.java:483)

   [testng] at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)

   [testng] at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)

   [testng] at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)

   [testng] at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)

   [testng] at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)

   [testng] at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)

   [testng] at org.testng.TestRunner.privateRun(TestRunner.java:756)

   [testng] at org.testng.TestRunner.run(TestRunner.java:610)

   [testng] at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)

   [testng] at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)

   [testng] at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)

   [testng] at org.testng.SuiteRunner.run(SuiteRunner.java:289)

   [testng] at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)

   [testng] at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)

   [testng] at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)

   [testng] at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)

   [testng] at org.testng.TestNG.runSuites(TestNG.java:1133)

   [testng] at org.testng.TestNG.run(TestNG.java:1104)

   [testng] at org.testng.TestNG.privateMain(TestNG.java:1434)

   [testng] at org.testng.TestNG.main(TestNG.java:1403)

   [testng] Caused by: java.lang.ClassCastException: com.saba.marketplace.entity.Vendor$MockitoMock$1143590419 cannot be cast to org.mockito.internal.creation.bytebuddy.MockAccess

   [testng] at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:48)

   [testng] at org.powermock.api.mockito.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:50)

   [testng] at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMethodInvocationControl(DefaultMockCreator.java:116)

   [testng] at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:69)

   [testng] at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46)

   [testng] at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:193)

   [testng] ... 36 more


The test runs fine if I extend the class with PowerMockTestCase instead of 

@ObjectFactory
    public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
    }


Reply all
Reply to author
Forward
0 new messages