简体   繁体   English

无法使用PowerMockRunner运行JUnit测试

[英]Unable to run JUnit test with PowerMockRunner

I have a Gradle based Java project were I now want to mock a private method using PowerMock. 我有一个基于Gradle的Java项目,我现在想要使用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. 问题是我无法使用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

This is a bug that occurs when you use JUnit 4.12 and PowerMock < 1.6.1. 这是使用JUnit 4.12和PowerMock <1.6.1时发生的错误。 The problem is solved in PowerMock 1.6.1. 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'

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. 你能不能再添加一些堆栈跟踪,它可以发现有关问题的更多细节。

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正确注入自身的能力。

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将不知道哪个类被嘲笑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM