Published 28 Aug, 2022

Java - PowerMock and Java 8 issue: InterfaceMethodrefInfo cannot be cast to MethodrefInfo

Category Java
Modified : Sep 29, 2022
75

I´m having issues while trying to execute a unit test using PowerMock with Mockito. I need PowerMockito to mock an static method.

These are the versions I´m using:

PowerMock 1.6.2
Mockito 1.10.19
JUnit 4.12
Java 8

When I add the annotation @PrepareForTest(Graph.class) I get the following error:

java.lang.IllegalStateException: Failed to transform class with name     name.of.my.package.GraphUtil. Reason: javassist.bytecode.InterfaceMethodrefInfo cannot be cast to javassist.bytecode.MethodrefInfo

I have read in the official PowerMock Google page that this is related to javassist. But I´m a bit lost and I don´t know how to fix it.

Just in case, I also tried downloading the latest SNAPSHOT of Powermock (1.6.3-SNAPSHOT) but did not work either.

Could anyone help me, please?

Thanks in advance

Answers

There are 4 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Powermock, Javassist. These have been categorized in sections for a clear and precise explanation.

42

Yes, that was the problem. PowerMock has a dependency to javassist, so I just had to exclude that transitive dependency in my pom and later include the dependency to the fixed version of javassist. And that worked for me. Thanks!


46

A Red Herring?

When I ran into this issue during Android development, I was a single test to an existing file.

Removing the test and re-adding one line at a time, I found that the underlying issue had to do with how TreeSet and its comparator were being instantiated.

YMMV.


36

Following Francisco González's answer, this is what I had to do :

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.5.5</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
        </exclusion>
    </exclusions>      
</dependency>
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.20.0-GA</version>
    <scope>test</scope>
</dependency>

20
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.22.0-GA</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
            </exclusion>
        </exclusions>
    </dependency>