简体   繁体   中英

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 . I am using Maven for dependency management.

IntelliJ idea keeps complaining that following imports cannot be resolved:

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>
    <!-- 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 ). 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). Try mvn clean install from the command line see if it gives any errors.

My IDE was not resolving JUnit & Mockito dependencies. Tried reimport in IntelliJ and mvn clean install multiple times which didn't help. 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
  2. Go to Project Folder and remove .idea folder (rm -rf ./idea)
  3. Import Maven project again (You will need to add back any run configurations that were deleted)

You can also use command

mvn -U idea:idea

This command forces a check for missing dependencies and helps in resolving them.

I solved this problem by adding mockito to module I used. Just go to File -> Project structure and in Modules add mockito to your selected module - the one in which you use mockito (usually Test ).

Restartig intellIJ 为我解决了这个问题

For me, I deleted org.mockito package from m2 repository and then did mvn clean install. this reimport the package.

Try using lower versions of junit and 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. (right-click on project folder -> Maven -> Reload project)

  • You can also try clicking on the tiny m symbol on the top right to
    load maven changes or select maven button to get clean+install
    options and click play button.

Maven 重新加载项目

Maven 加载和全新安装选项

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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