无法使用PowerMockRunner运行JUnit测试 [英] Unable to run JUnit test with PowerMockRunner

查看:1570
本文介绍了无法使用PowerMockRunner运行JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Gradle的Java项目,现在我想使用PowerMock来模拟一个私有的方法。问题是我不能使用PowerMockRunner,因为我添加 @RunWith(org.powermock.modules.junit4.PowerMockRunner.class)注释。



错误:

  org.powermock.reflect。 exceptions.FieldNotFoundException:在org.junit.internal.runners.MethodValidator类中找不到字段'fTestClass'。 
在org.powermock.reflect.internal.WhiteboxImpl.getInternalState(WhiteboxImpl.java:581)
在org.powermock.reflect.Whitebox.getInternalState(Whitebox.java:308)
在org .powermock.modules.junit4.internal.impl.testcaseworkaround.PowerMockJUnit4MethodValidator.validate TestMethods(PowerMockJUnit4MethodValidator.java:79)
在org.powermock.modules.junit4.internal.impl.testcaseworkaround.PowerMockJUnit4MethodValidator.validate InstanceMethods(PowerMockJUnit4MethodValidator。 java:49)
在org.junit.internal.runners.MethodValidator.validateMethodsForDefaultRunner(MethodValidator.java:51)
在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'

测试本身也完全失效(初始化错误)失败:

  @RunWith(PowerMockRunner.class)
public class SomeTest {
@Test
public void testSomething(){
}
}

任何想法可能是错的?使用PowerMock的其他测试工作正常(没有人使用PowerMockRunner)。



请问和谢谢任何帮助!

解决方案

这是一个错误,当您使用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'

如果您无法升级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'

你可以添加更多的stacktrace行,这样可以发现更多有关问题的细节。


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.

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).

Greetings and thanks for any help! Ben

解决方案

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'

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆