cannot resolve symbol PowerMockRunner

62,437

Solution 1

You need to import PowerMockRunner as follows:

import org.powermock.modules.junit4.PowerMockRunner;

Solution 2

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.

Solution 3

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"

Solution 4

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>

Solution 5

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

Share:
62,437

Related videos on Youtube

Elad Benda2
Author by

Elad Benda2

Updated on July 09, 2022

Comments

  • Elad Benda2
    Elad Benda2 almost 2 years

    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 ?

    • David Conrad
      David Conrad almost 10 years
      Because you imported PrepareForTest, but you forgot to import PowerMockRunner?
    • Elad Benda2
      Elad Benda2 over 9 years
      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?
    • David Conrad
      David Conrad over 9 years
      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.
    • Elad Benda2
      Elad Benda2 over 9 years
      thanks. please write an answer and I'll vote you
  • Muthaiah PL
    Muthaiah PL about 6 years
    I already have this import. But still i didn't work. I have the jar as well.
  • Muthaiah PL
    Muthaiah PL about 6 years
    I already have the import and the jar. But still i didn't work.