Java 无法使用 PowerMockRunner 运行 JUnit 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26192929/
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
Unable to run JUnit test with PowerMockRunner
提问by Ben
I have a Gradle based Java project were I now want to mock a private method using PowerMock. The problem is that I am not able to use the PowerMockRunner as I always get the following exception when I add the @RunWith(org.powermock.modules.junit4.PowerMockRunner.class)
annotation.
我有一个基于 Gradle 的 Java 项目,我现在想使用 PowerMock 模拟一个私有方法。问题是我无法使用 PowerMockRunner,因为在添加@RunWith(org.powermock.modules.junit4.PowerMockRunner.class)
注释时总是出现以下异常。
Error:
错误:
org.powermock.reflect.exceptions.FieldNotFoundException: Field 'fTestClass' was not found in class org.junit.internal.runners.MethodValidator.
at org.powermock.reflect.internal.WhiteboxImpl.getInternalState(WhiteboxImpl.java:581)
at org.powermock.reflect.Whitebox.getInternalState(Whitebox.java:308)
at org.powermock.modules.junit4.internal.impl.testcaseworkaround.PowerMockJUnit4MethodValidator.validate TestMethods(PowerMockJUnit4MethodValidator.java:79)
at org.powermock.modules.junit4.internal.impl.testcaseworkaround.PowerMockJUnit4MethodValidator.validate InstanceMethods(PowerMockJUnit4MethodValidator.java:49)
at org.junit.internal.runners.MethodValidator.validateMethodsForDefaultRunner(MethodValidator.java:51)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.validate(PowerMockJUnit44RunnerDelegateImpl.java:108)
...
This are my test dependencies:
这是我的测试依赖项:
testCompile 'junit:junit:4.+',
'org.powermock:powermock-core:1.5.6',
'org.powermock:powermock-module-junit4:1.5.6',
'org.powermock:powermock-api-mockito:1.5.6'
The test itself fails also when completely empty (initialization error):
当完全为空时,测试本身也会失败(初始化错误):
@RunWith(PowerMockRunner.class)
public class SomeTest {
@Test
public void testSomething() {
}
}
Any ideas what might be wrong? Other tests using PowerMock are working fine (none of them uses the PowerMockRunner).
任何想法可能有什么问题?其他使用 PowerMock 的测试工作正常(它们都没有使用 PowerMockRunner)。
Greetings and thanks for any help! Ben
问候并感谢您的帮助!本
采纳答案by Stefan Birkner
This is a bug that occurs when you use JUnit 4.12 and PowerMock < 1.6.1. The problem is solved in PowerMock 1.6.1. Please update your dependencies accordingly
这是使用 JUnit 4.12 和 PowerMock < 1.6.1 时发生的错误。这个问题在 PowerMock 1.6.1 中得到解决。请相应地更新您的依赖项
testCompile 'junit:junit:4.12',
'org.powermock:powermock-core:1.6.1',
'org.powermock:powermock-module-junit4:1.6.1',
'org.powermock:powermock-api-mockito:1.6.1'
If you cannot upgrade PowerMock then you can use JUnit 4.11.
如果您无法升级 PowerMock,那么您可以使用 JUnit 4.11。
testCompile 'junit:junit:4.11',
'org.powermock:powermock-core:1.5.6',
'org.powermock:powermock-module-junit4:1.5.6',
'org.powermock:powermock-api-mockito:1.5.6'
Could you please add further lines of the stacktrace, which uncover more details about the problem.
您能否添加更多的堆栈跟踪行,以揭示有关该问题的更多详细信息。
回答by SonalKhodiyar
Check what Stefan said, and above that you also need to add
检查 Stefan 所说的内容,此外您还需要添加
@PrepareForTest({<The class/es you are Mocking>, ...})
without the prepare for test, PowerMockRunner won't know which class is mocked.
如果没有准备测试,PowerMockRunner 将不知道哪个类被模拟。
回答by Brandon Heck
There has been a bug logged against PowerMock: https://code.google.com/p/powermock/issues/detail?id=531
有一个针对 PowerMock 的错误记录:https: //code.google.com/p/powermock/issues/detail?id =531
It appears that JUnit changed some of its internal field names that PowerMock was accessing via reflection, thus breaking the ability for PowerMock to properly inject itself.
JUnit 似乎更改了 PowerMock 通过反射访问的一些内部字段名称,从而破坏了 PowerMock 正确注入自身的能力。