Java 为静态模拟设置 Powemockito
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19055650/
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
Setting up Powemockito for static mocking
提问by Ubunfu
I would like to make use of Powermock with Mockito to mock some static method calls. I have followed instructions and examples from SO as well as the PowerMock Getting Startedand MockStaticpages as best I can, but I am yet to complete a mockStatic() call.
我想使用 Powermock 和 Mockito 来模拟一些静态方法调用。我已尽我所能遵循 SO 以及 PowerMock入门和MockStatic页面的说明和示例,但我还没有完成 mockStatic() 调用。
When I call mockStatic(foo.class) from my test class, I'm given the excception:
当我从我的测试类调用 mockStatic(foo.class) 时,我得到了例外:
java.lang.NoClassDefFoundError: org/Mockito/mock/MockName
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)
at ...my test class method call...
I'm sure this is a setup problem, as I have been finding the terminology used for setting this up to be pretty confusing. I did grab the Mockito Zip from the PowerMock downloads. In Eclipse (3.5.2) I opened the project properties and added all of the Jars to the build path. I also tried adding the entire unzipped powermockito folder to my environment vars classpath, and then just the powermockito jar specifically when that didn't work out.
我确定这是一个设置问题,因为我发现用于设置的术语非常混乱。我确实从 PowerMock下载中获取了 Mockito Zip 。在 Eclipse (3.5.2) 中,我打开了项目属性并将所有的 Jars 添加到构建路径中。我还尝试将整个解压缩的 powermockito 文件夹添加到我的环境变量类路径中,然后在没有解决的情况下专门添加 powermockito jar。
I have these annotationsat the class-level of my test class as well, per the powermock instructions:
根据 powermock 说明,我在测试类的类级别也有这些注释:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ApplicationContextLoader.class)
Also these powermock-specific imports:
还有这些特定于 powermock 的导入:
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
To those of you who have used PowerMockito before, even just a pointer in the right direction, or something to check would be really helpful. I'm struggling to see how my setup differs from that of posts I have seen using - from what I can tell - the same syntax.
对于那些之前使用过 PowerMockito 的人来说,即使只是指向正确方向的指针,或者需要检查的东西也会非常有帮助。我正在努力了解我的设置与我所看到的帖子有何不同 - 据我所知 - 相同的语法。
回答by Walls
If you are using a static mock object, in your PrepareForTest
annotation, add the class that is USING the static object in addition to the static class itself. If the class you are testing needs to use this static, add the current class to the annotation. You don't actually mock the class, but it needs to be in the annotation for the static to hook in. It sounds weird, but it works.
如果您使用的是静态模拟对象,请在PrepareForTest
注释中添加使用静态对象的类以及静态类本身。如果您正在测试的类需要使用此静态,请将当前类添加到注释中。你实际上并没有模拟这个类,但它需要在注释中才能让静态挂钩。这听起来很奇怪,但它确实有效。
When adding multiple classes into the annotation you can have them inside {}
and seperated by commas. For example if your static class is StaticA.class
and the class using the static is CallerOfStatic.class
you can use:
在注释中添加多个类时,您可以将它们放在里面{}
并用逗号分隔。例如,如果您的静态类是StaticA.class
,而使用静态的类是CallerOfStatic.class
您可以使用:
@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticA.class, CallerOfStatic.class})
回答by Devs
Probably, the versions of Powermock and Mockito are not compatible. Fix that and it won't be a n issue anymore.
可能是 Powermock 和 Mockito 的版本不兼容。解决这个问题,它就不再是问题了。
Mockito PowerMock
1.10.8+ 1.6.2+
1.9.5-rc1 - 1.9.5 1.5.0 - 1.5.6
1.9.0-rc1 & 1.9.0 1.4.10 - 1.4.12
1.8.5 1.3.9 to 1.4.9
1.8.4 1.3.7 & 1.3.8
1.8.3 1.3.6
1.8.1 & 1.8.2 1.3.5
1.8 1.3
1.7 1.2.5
See: https://github.com/powermock/powermock/wiki/Mockito#supported-versions
请参阅:https: //github.com/powermock/powermock/wiki/Mockito#supported-versions
回答by prayagupd
Make sure powermockito and mockito versions aligned as in this chart - https://github.com/powermock/powermock/wiki/Mockito#supported-versions,
确保 powermockito 和 mockito 版本如该图表所示对齐 - https://github.com/powermock/powermock/wiki/Mockito#supported-versions,
Easy way to find is,
容易找到的方法是,
mvn dependency:tree | grep mockito
[INFO] | \- org.mockito:mockito-core:jar:1.8.5:compile
[INFO] +- org.mockito:mockito-all:jar:1.9.5:compile
[INFO] +- org.powermock:powermock-api-mockito:jar:1.5.6:compile
In my case, powermock 1.5.6
and mockito 1.9.5
were aligned but had to change to use mockito 1.8.5
, as someone else in the dependency was already using mockito 1.8.5.
就我而言,powermock1.5.6
和 mockito1.9.5
是对齐的,但必须更改为 use mockito 1.8.5
,因为依赖项中的其他人已经在使用 mockito 1.8.5。
Following combination perfectly works for me,
以下组合非常适合我,
mvn dependency:tree | grep mockito
[INFO] | \- org.mockito:mockito-core:jar:1.8.5:compile
[INFO] +- org.mockito:mockito-all:jar:1.8.5:compile
[INFO] +- org.powermock:powermock-api-mockito:jar:1.4.9:compile