eclipse 找不到导入的 org.mockito.Mock

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36469859/
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-09-19 23:07:01  来源:igfitidea点击:

The import org.mockito.Mock cannot be found

javaeclipsespringgradle

提问by lukegjpotter

I am experiencing a problem I hope you can help with.

我遇到了一个问题,希望你能帮忙。

I want to use Mockito in my Spring Boot w/Gradle project, but STS cannot resolve the dependancy.

我想在我的 Spring Boot w/Gradle 项目中使用 Mockito,但 STS 无法解决依赖关系。

I have the following in my build.gradlefile;

我的build.gradle文件中有以下内容;

repositories { jcenter() }
dependencies { testCompile('org.mockito:mockito-core:1.+') }

When I do a ./gradlew --info buildI can see that it is resolving Mockito:

当我执行 a 时,./gradlew --info build我可以看到它正在解析 Mockito:

Resolved versions: {org.mockito:mockito-core=1.+}
Using version '1.+' for dependency 'org.mockito:mockito-core:1.+'
Using version '1.+' for dependency 'org.mockito:mockito-core:1.10.19'

After a ./gradlew cleanEclipse eclipseit is in my STS Project's Build Path

之后./gradlew cleanEclipse eclipse它在我的 STS 项目的构建路径中

In Build Path

在构建路径中

My Code File is showing the following message:

我的代码文件显示以下消息:

cannot resolve Mockito

无法解析 Mockito

I have another project, setup in exactly the same way and it is working fine.

我有另一个项目,以完全相同的方式设置,并且工作正常。

Please help me out guys, Luke.

请帮帮我,卢克。

回答by

Use with a static import:

与静态导入一起使用:

import static org.mockito.Mockito.when;   ...or...
import static org.mockito.Mockito.*; 

回答by nnedoklanov

To add to @Harshad's response, you can make your life easier and let Eclipse handle the static imports by adding the mockito methods to your favorite imports enter image description here

要添加到@Harshad 的响应中,您可以通过将 mockito 方法添加到您最喜欢的导入来让您的生活更轻松并让 Eclipse 处理静态导入 在此处输入图片说明

This will then result in the following hint: enter image description here

这将导致以下提示: 在此处输入图片说明