PowerMock 和 Java 8 问题:InterfaceMethodrefInfo 不能转换为 MethodrefInfo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31189086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PowerMock and Java 8 issue: InterfaceMethodrefInfo cannot be cast to MethodrefInfo
提问by KurroGB
I′m having issues while trying to execute a unit test using PowerMock with Mockito. I need PowerMockito to mock an static method.
我在尝试使用 PowerMock 和 Mockito 执行单元测试时遇到问题。我需要 PowerMockito 来模拟静态方法。
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:
当我添加注释 @PrepareForTest(Graph.class) 时,出现以下错误:
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.
我在官方 PowerMock Google 页面中读到这与 javassist 相关。但我有点迷茫,我不知道如何修复它。
Just in case, I also tried downloading the latest SNAPSHOT of Powermock (1.6.3-SNAPSHOT) but did not work either.
以防万一,我也尝试下载 Powermock 的最新 SNAPSHOT (1.6.3-SNAPSHOT) 但也没有奏效。
Could anyone help me, please?
请问有人可以帮我吗?
Thanks in advance
提前致谢
采纳答案by KurroGB
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!
是的,这就是问题所在。PowerMock 依赖于 javassist,所以我只需要在我的 pom 中排除该传递依赖,然后将依赖包含到 javassist 的固定版本。这对我有用。谢谢!
回答by Prashant
Following Francisco González'sanswer, 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>
回答by DavidAyala
<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>
回答by steve-gregory
A Red Herring?
红鲱鱼?
When I ran into this issue during Android development, I was a single test to an existing file.
当我在 Android 开发过程中遇到这个问题时,我是对现有文件的单一测试。
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.
删除测试并一次重新添加一行,我发现根本问题与 TreeSet 及其比较器的实例化方式有关。
YMMV.
天啊。