1

We're upgrading from Java 8 to 11. After having done all the necessary SDK changes, I started running our unit tests using maven. All tests that use Mockito fail with

java.lang.ClassCastException: 
class org.mockito.codegenEventapiManager$MockitoMock$1091821173 cannot be cast to class
org.mockito.internal.creation.bytebuddy.MockAccess
(org.mockito.codegenEventapiManager$MockitoMock$1091821173 is in unnamed module of loader
org.powermock.core.classloader.javassist.JavassistMockClassLoader @1473b8c0;
org.mockito.internal.creation.bytebuddy.MockAccess is in unnamed module of loader 'app')
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:48)
    at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:25)
    at org.powermock.api.mockito.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:41)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
    at org.mockito.Mockito.mock(Mockito.java:1896)
    at org.mockito.Mockito.mock(Mockito.java:1805)
    [...line where we cal Mockito, rest is ommitted]

Could this be related to a version number mismatch in some of the dependencies?

We're using

mockito-core 2.24.0, excluding byte-buddy, byte-buddy-agent and objenesis
powermock-module-testng 2.0.4 (excluding above)
powermock-api-mockito2 2.0.4
objenesis 3.1
byte-buddy 1.9.7
byte-buddy-agent 1.9.7

Mocking the class is straight forward:

EventapiManager mockManager = Mockito.mock(EventapiManager.class);

The mocked class itself is public and everything is working fine on Java 8.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
RekaB
  • 438
  • 5
  • 14
  • 1
    Just try using the latest version of byte-buddy ` net.bytebuddy byte-buddy 1.10.7 ` – Naman Jan 27 '20 at 16:49
  • Tried, but I get the same result, unfortunately. I tried the latest version of `mockito-core` as well, no difference. I tried excluding `mockito-core` entirely and use the built-in version from PowerMock, the path is slightly different but the result is the same. – RekaB Jan 28 '20 at 08:21
  • Probably a problem with PowerMock. I'd suggest to ask on their issue tracker. – Rafael Winterhalter Jan 28 '20 at 12:08

1 Answers1

3

I have resolved my issue in an unexpected way: while googling for solutions I came across this comment, which describes another failing test that I had. In that test fixture, we indeed use the PowerMock annotation described in the comment, but had none of the ignored packages suggested there. So I added them, making the complete list of ignored packages:

@PowerMockIgnore({"javax.management.*", "javax.script.*", "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})

This has mysteriously resolved ALL failures, including the above that had its fixture in a different package.

RekaB
  • 438
  • 5
  • 14
  • Thanks, this strangely enough worked for me as well when I used the same ignore list across all of my test classes. – John Lyon Oct 17 '21 at 23:20