Failed to start JUnit test using PowerMockRunner

I have a Gradle based Java project, now I want to mock my personal method using PowerMock. The problem is that I cannot use PowerMockRunner, since I always get the following exception when I add @RunWith(org.powermock.modules.junit4.PowerMockRunner.class) annotation @RunWith(org.powermock.modules.junit4.PowerMockRunner.class) .

Mistake:

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

These 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 also fails when it is completely empty (initialization error):

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

Any ideas what could be wrong? Other tests using PowerMock work fine (none of them use PowerMockRunner).

Greetings and thanks for any help! Ben

+44
java exception junit powermock
04 Oct '14 at 12:41
source share
3 answers

This is an error that occurs when using JUnit 4.12 and PowerMock <1.6.1. The problem is resolved 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, 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 add additional stacktrace lines that reveal more detailed information about the problem.

+63
Oct 06 '14 at 18:45
source share

An error occurred with PowerMock: https://code.google.com/p/powermock/issues/detail?id=531

It seems that JUnit has changed some of its internal field names that PowerMock accessed through reflection, thereby disrupting PowerMock's ability to correctly enter itself.

+1
Dec 12 '14 at 2:33
source share

See what Stefan said, and above that you also need to add

 @PrepareForTest({<The class/es you are Mocking>, ...}) 

without preparation for testing, PowerMockRunner will not know which class is mocking.

0
Oct. 06 '14 at 18:57
source share



All Articles