45

I'm trying to use Powermock for the first time

I use build.gradle and added:

dependencies {
...
    testCompile 'org.mockito:mockito-all:1.9.5'
    testCompile 'org.powermock:powermock-api-mockito:1.5.5'

}

now I look at my test class which has:

import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.powermock.core.classloader.annotations.PrepareForTest;


@RunWith(PowerMockRunner.class)
@PrepareForTest(GeoUtils.class)

and get this error:

@RunWith(PowerMockRunner.class)
             ^
cannot resolve symbol PowerMockRunner 

how come it resolves PrepareForTest and not PowerMockRunner ?

4
  • Because you imported PrepareForTest, but you forgot to import PowerMockRunner? Jul 31, 2014 at 15:19
  • it doesn't give me any suggestion to PowerMockRunner import as it did with PrepareForTest. So I guess it cannot resolve its import anyhow. no?what is it? Jul 31, 2014 at 21:56
  • 2
    I believe it should be org.powermock.modules.junit4.PowerMockRunner. If it doesn't accept the import, the dependency must not have been resolved for some reason. Jul 31, 2014 at 22:07
  • thanks. please write an answer and I'll vote you Aug 1, 2014 at 12:08

7 Answers 7

42

PowerMockRunner is part of powermock-module-junit4.jar. You need to explicity import this jar

You could specify the dependency for this jar as per your requirement. Refer to this link.

1
  • 1
    I already have the import and the jar. But still i didn't work. Mar 4, 2018 at 21:29
40

You need to import PowerMockRunner as follows:

import org.powermock.modules.junit4.PowerMockRunner;
1
  • 10
    I already have this import. But still i didn't work. I have the jar as well. Mar 4, 2018 at 21:28
26

You just need to add the gradle dependency

testCompile "org.powermock:powermock-module-junit4:1.6.4"

or if you're use Android Studio version 3+

testImplementation "org.powermock:powermock-module-junit4:1.6.4"
13

I faced this error because I had only the first of the following dependencies added in my pom.xml. Please ensure you add both the following maven dependencies for PowerMock to your pom.xml.

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.3</version>
</dependency>
3

I also faced with a similar error. Although the jars are already in the classpath, eclipse didn't suggest to import PowerMockRunner class. It was giving error "Class cannot be resolved to a type". I had to manually add the import "org.powermock.modules.junit4.PowerMockRunner".

1

I came across exactly the same problem just now and both the dependencies were already included in the pom.xml.

For me, it turned out to be Intellij caching issue and invalidating the cache worked for me.

Here are the steps:

IntelliJ -> File Menu -> Invalidate Caches -> Invalidate and Restart

0

For instrumental tests you have to use androidTestImplementation "org.powermock:powermock-module-junit4:1.6.4"

But then I found out, you can´t use PowerMock with instrumental tests:

PowerMock won't work on Android if you run it on a device since PowerMock is using JVM byte code manipulation. It will work if you run it on a JVM though.

(link to this problem)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.