Java 无法解析符号 PowerMockRunner

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25062082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:32:31  来源:igfitidea点击:

cannot resolve symbol PowerMockRunner

javaunit-testingmockingmockitopowermock

提问by Elad Benda2

I'm trying to use Powermock for the first time

我第一次尝试使用 Powermock

I use build.gradle and added:

我使用 build.gradle 并添加:

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 PrepareForTestand not PowerMockRunner?

它怎么解决PrepareForTest而不是PowerMockRunner

采纳答案by David Conrad

You need to import PowerMockRunneras follows:

您需要PowerMockRunner按如下方式导入:

import org.powermock.modules.junit4.PowerMockRunner;

回答by Balaji Katika

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

PowerMockRunner 是powermock-module-junit4.jar. 您需要明确导入此 jar

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

您可以根据您的要求指定此 jar 的依赖项。请参阅此链接

回答by maruti060385

You just need to add the gradle dependency

你只需要添加gradle依赖

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

or if you're use Android Studio version 3+

或者如果您使用的是 Android Studio 版本 3+

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

回答by wateva

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.

我遇到了这个错误,因为我的 pom.xml 中只添加了以下依赖项中的第一个。请确保将 PowerMock 的以下 Maven 依赖项添加到 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>

回答by pavithraCS

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

我也遇到了类似的错误。尽管 jars 已经在类路径中,但 eclipse 并没有建议导入 PowerMockRunner 类。它给出了错误“类无法解析为类型”。我不得不手动添加导入“ org.powermock.modules.junit4.PowerMockRunner”。

回答by Bhola Bhala

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

对于仪器测试,您必须使用 androidTestImplementation "org.powermock:powermock-module-junit4:1.6.4"

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

但后来我发现,你不能在仪器测试中使用 PowerMock:

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.

如果您在设备上运行 PowerMock,则它无法在 Android 上运行,因为 PowerMock 使用的是 JVM 字节码操作。但是,如果您在 JVM 上运行它,它将起作用。

(link to this problem)

此问题的链接