3

After successfully integrating PowerMock into my project, I ran into this error: ClassCastException occurred while creating the mockito mock. I found a solution, but it does not work. When running tests, an error occurs in the class that mocking some JRE classes. If I delete the tests in which I use verifyStatic (), then the error disappears. If I run the test with an error separately, everything works. I tried:

1) In Core module, in tests folder, I add package org.mockito.configuration and MockitoConfigutation. If I run the debugger and set the stop point to the enableClassCache() method, it stops when I run the tests, which means that this class works as intended.

2) Add @PowerMockIgnore({"*.*"}) annotation on class with error.

Example class with error:

@PowerMockIgnore({"*.*"})
public class PersonTest extends AbsTest {
    @BeforeClass
    protected void setUp() throws Exception {
        mock(ArrayList.class);
    }
}

build.gradle on Core module:

...
testCompile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
testCompile 'org.testng:testng:6.13.1'
testCompile "org.mockito:mockito-core:2.8.9"
testCompile "org.powermock:powermock-api-mockito2:1.7.3"
testCompile "org.powermock:powermock-module-testng:1.7.3"
testCompileOnly "org.projectlombok:lombok:1.16.18"
...

Error:

org.mockito.exceptions.base.MockitoException: ClassCastException occurred while creating the mockito mock : class to mock : 'com.brashmonkey.spriter.Animation', loaded by classloader : 'sun.misc.Launcher$AppClassLoader@18b4aac2' created class : 'com.brashmonkey.spriter.Animation$MockitoMock$1309622717', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@185f7840'
proxy instance class : 'com.brashmonkey.spriter.Animation$MockitoMock$1309622717', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@185f7840'
instance creation by : ObjenesisInstantiator

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

at ru.coolone.adventure_emulation.scripts.person.PersonTest.setUpClass(PersonTest.java:160) 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:124) at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:451) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142) at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:163) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:105) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187) at org.testng.TestNG.runSuitesLocally(TestNG.java:1116) at org.testng.TestNG.runSuites(TestNG.java:1028) at org.testng.TestNG.run(TestNG.java:996) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127) Caused by: java.lang.ClassCastException: com.brashmonkey.spriter.Animation$MockitoMock$1309622717 cannot be cast to org.mockito.internal.creation.bytebuddy.MockAccess at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:48) at org.powermock.api.mockito.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:50) at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMethodInvocationControl(DefaultMockCreator.java:116) at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:69) at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46) at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:138) ... 26 more

CoolONE
  • 89
  • 4
  • 8

1 Answers1

1

Ok, so I had same behavior, it is the line:

@PowerMockIgnore({"*.*"})

This will ignore org.mockito.* which causes the issue. Be more specific about what is ignored, for instance:

@PowerMockIgnore({"org.robolectric.", "android.", "com.sun.org.apache.xerces.internal.jaxp.*"})

Haider Saleem
  • 773
  • 1
  • 9
  • 17