简体   繁体   English

IntelliJ Idea 未解析 Mockito 和 JUnit 与 Maven 的依赖关系

[英]IntelliJ Idea not resolving Mockito and JUnit dependencies with Maven

I am using IntelliJ idea for writing simple unit test cases using JUnit and Mockito .我正在使用IntelliJ idea使用JUnitMockito编写简单的单元测试用例。 I am using Maven for dependency management.我正在使用 Maven 进行依赖管理。

IntelliJ idea keeps complaining that following imports cannot be resolved: IntelliJ idea一直抱怨无法解决以下导入:

import org.junit.Test; //Cannot resolve symbol 'Test'

import static org.mockito.Mockito.*; //Cannot resolve symbol 'mockito'

Following is the dependencies section of my project:以下是我项目的dependencies项部分:

<dependencies>
    <!-- Dependency for JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <!--<scope>test</scope>-->
    </dependency>
    <!-- Dependency for Mockito -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <!--<scope>test</scope>-->
    </dependency>
</dependencies>

Following is my project structure:以下是我的项目结构:

项目结构

Try View -> Tool Windows -> Maven projects , then click the blue icon in the top left of the tool window ( Reimport all Maven projects ).尝试View -> Tool Windows -> Maven projects ,然后单击工具窗口左上角的蓝色图标( Reimport all Maven projects )。 You should be able to find the dependencies in the project view, under external libraries.您应该能够在项目视图中的外部库下找到依赖项。

If this does not work, there is probably something wrong with your maven config (pom.xml).如果这不起作用,则您的 maven 配置 (pom.xml) 可能有问题。 Try mvn clean install from the command line see if it gives any errors.从命令行尝试mvn clean install看看它是否有任何错误。

My IDE was not resolving JUnit & Mockito dependencies.我的 IDE 没有解决 JUnit 和 Mockito 依赖项。 Tried reimport in IntelliJ and mvn clean install multiple times which didn't help.多次尝试在 IntelliJ 和 mvn clean install 中重新导入,但没有帮助。 Reimport worked for me, but with the following steps.重新导入对我有用,但需要执行以下步骤。

Please be aware that you will lose any run configurations that you have created.请注意,您将丢失您创建的所有运行配置。

  1. Close IntelliJ关闭 IntelliJ
  2. Go to Project Folder and remove .idea folder (rm -rf ./idea)转到项目文件夹并删除 .idea 文件夹(rm -rf ./idea)
  3. Import Maven project again (You will need to add back any run configurations that were deleted)再次导入 Maven 项目(您需要重新添加已删除的任何运行配置)

You can also use command您也可以使用命令

mvn -U idea:idea mvn -U 想法:想法

This command forces a check for missing dependencies and helps in resolving them.此命令强制检查缺少的依赖项并帮助解决它们。

I solved this problem by adding mockito to module I used.我通过将 mockito 添加到我使用的模块中解决了这个问题。 Just go to File -> Project structure and in Modules add mockito to your selected module - the one in which you use mockito (usually Test ).只需转到File -> Project structure并在Modules中将 mockito 添加到您选择的模块 - 您在其中使用 mockito 的模块(通常是Test )。

Restartig intellIJ 为我解决了这个问题

For me, I deleted org.mockito package from m2 repository and then did mvn clean install.对我来说,我从 m2 存储库中删除了 org.mockito 包,然后做了 mvn clean install。 this reimport the package.这重新导入包。

Try using lower versions of junit and mockito.尝试使用较低版本的 junit 和 mockito。

The latest versions weren't working in my project but it worked once I used lower versions.最新版本在我的项目中不起作用,但在我使用较低版本后它就起作用了。

Few other things you can do:您可以做的其他几件事:

  • After adding the dependency just make sure to update the maven project.添加依赖项后,只需确保更新 maven 项目。 (right-click on project folder -> Maven -> Reload project) (右键单击项目文件夹 -> Maven -> 重新加载项目)

  • You can also try clicking on the tiny m symbol on the top right to您也可以尝试点击右上角的小 m 符号来
    load maven changes or select maven button to get clean+install加载 maven 更改或 select maven 按钮以进行清理+安装
    options and click play button.选项,然后单击播放按钮。

Maven 重新加载项目

Maven 加载和全新安装选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM