21

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

KurroGB
  • 431
  • 2
  • 4
  • 8
  • 1
    Well in the issue tracker of javassist there was a bug related (https://issues.jboss.org/browse/JASSIST-220) to that exception and Mr Chiba merged a pull request on the 22 July 2014 (https://github.com/jboss-javassist/javassist/pull/11). My first suggestion would be to make sure you're using the latest version of javassist and if upgrade to latest version. – pabrantes Jul 07 '15 at 09:29
  • please add that comment as your own answer and accept it so the question doesn't stay unanswered. Thank you :) – pabrantes Jul 08 '15 at 15:40

4 Answers4

24

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>
Community
  • 1
  • 1
Prashant
  • 923
  • 1
  • 11
  • 13
  • 5
    It *does NOT work* if another dependency of your project depends on `javassist` (e.x. hibernate). You have to remove `test` when you add `javassist` dependency. – Arashsoft Jul 05 '17 at 21:19
5

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!

KurroGB
  • 431
  • 2
  • 4
  • 8
  • 7
    mention specific versions with problem and fix, and sample pom would help people a lot. – Andrew Mackenzie Mar 28 '16 at 12:06
  • Actually you do not need to exclude an explicitly overridden transitive dependency in your POM. Probably you do not even need to declare it as a direct dependency at all but use the transitive one, if you version-manage it according to your requirement in your POM's `` section. – kriegaex Jul 10 '20 at 05:28
5
    <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>
DavidAyala
  • 51
  • 1
  • 2
0

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.

steve-gregory
  • 7,396
  • 8
  • 39
  • 47