问题描述
我想将Powermock与Mockito结合使用来模拟一些静态方法调用.我遵循了SO以及PowerMock 入门和页面,但我还没有完成一次嘲笑(Static)调用.
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 Started and MockStatic pages as best I can, but I am yet to complete a mockStatic() call.
当我从测试类中调用mockStatic(foo.class)时,我得到了一个提示:
When I call mockStatic(foo.class) from my test class, I'm given the excception:
java.lang.NoClassDefFoundError: org/Mockito/mock/MockName
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)
at ...my test class method call...
我确定这是一个设置问题,因为我发现用于设置此设置的术语非常混乱.我确实从PowerMock 下载中获取了Mockito邮编.在Eclipse(3.5.2)中,我打开了项目属性,并将所有Jars添加到了构建路径.我还尝试将整个解压缩的powermockito文件夹添加到我的环境vars类路径中,然后仅将powermockito jar添加到我的环境中(如果无法解决该问题).
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的说明,我也在测试类的类级别上具有这些批注:
I have these annotationsat the class-level of my test class as well, per the powermock instructions:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ApplicationContextLoader.class)
还有这些powermock特定的导入:
Also these powermock-specific imports:
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
对于那些以前使用过PowerMockito的人来说,即使只是指向正确方向的指针或需要检查的内容也将非常有帮助.我正在努力查看自己的设置与我看到的使用相同语法的帖子有何不同.
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.
推荐答案
Powermock和Mockito的版本可能不兼容.解决这个问题,这将不再是一个问题.
Probably, the versions of Powermock and Mockito are not compatible. Fix that and it won't be a n issue anymore.
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
请参阅: https://github.com/powermock/powermock/wiki /Mockito#supported-versions
这篇关于设置Powemockito进行静态模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!