无法使用 PowerMockRunner 运行 JUnit 测试 [英] Unable to run JUnit test with PowerMockRunner
问题描述
我有一个基于 Gradle 的 Java 项目,现在我想使用 PowerMock 模拟私有方法.问题是我无法使用 PowerMockRunner,因为在添加 @RunWith(org.powermock.modules.junit4.PowerMockRunner.class)
注释时总是出现以下异常.
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.
错误:
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)
...
这是我的测试依赖项:
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() {
}
}
任何想法可能是错误的?其他使用 PowerMock 的测试工作正常(没有一个使用 PowerMockRunner).
Any ideas what might be wrong? Other tests using PowerMock are working fine (none of them uses the PowerMockRunner).
您好,感谢您的帮助!本
Greetings and thanks for any help! Ben
推荐答案
这是在使用 JUnit 4.12 和 PowerMock 时发生的错误1.6.1.这个问题在 PowerMock 1.6.1 中得到解决.请相应地更新您的依赖项
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
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'
如果您无法升级 PowerMock,那么您可以使用 JUnit 4.11.
If you cannot upgrade PowerMock then you can use 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.
这篇关于无法使用 PowerMockRunner 运行 JUnit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!