RekaB
RekaB

Reputation: 448

Exception when using Mockito with Java11

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.

Upvotes: 1

Views: 5998

Answers (1)

RekaB
RekaB

Reputation: 448

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.

Upvotes: 3

Related Questions