Unable to run JUnit test with PowerMockRunner

JavaExceptionJunitPowermock

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

There has been a bug logged against 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.

Solution 3 - Java

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

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

without the prepare for test, PowerMockRunner won't know which class is mocked.

Solution 4 - Java

There may also exist dependencies on classpath that override a JUnit specific class which contains JUnit's version. This leads to incorrect version comparison results in PowerMock. For instance, I had com.google.android.tools:dx:1.7 on classpath (came from hunspell library). It overrides following method return result:

junit.runner.Version.id() => "3.8.1"

Usually it should return something like "4.12" or "4.11" etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenView Question on Stackoverflow
Solution 1 - JavaStefan BirknerView Answer on Stackoverflow
Solution 2 - JavaBrandon HeckView Answer on Stackoverflow
Solution 3 - JavaSonalKhodiyarView Answer on Stackoverflow
Solution 4 - JavaPAXView Answer on Stackoverflow