IntelliJ Idea not resolving Mockito and JUnit dependencies with Maven

18,224

Solution 1

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.

Solution 2

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)

Solution 3

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

Share:
18,224

Related videos on Youtube

Yash
Author by

Yash

Updated on September 15, 2022

Comments

  • Yash
    Yash over 1 year

    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:

    Project Structure

    • Bob
      Bob
      Try to right click on you project, then in the menu -> maven -> Download Sources (or Reimport)
  • Jan B.
    Jan B. over 6 years
    Because sometimes Idea has hick-ups :)
  • Yash
    Yash over 6 years
    Some hiccup @Matt!
  • Tobb
    Tobb over 6 years
    Usually when you change a pom-file tracked by IDEA, you get a small popup in your lower right corner asking you if you want to reimport the project (or enable autoimport). Changes done to the pom file has no effect until a reimport is completed.
  • Yash
    Yash over 6 years
    @Tobb Yeah! That's the usual case, but I was not getting that pop-up in this case. However, the Reimport worked for me. Thanks again :)
  • Tobb
    Tobb about 5 years
    The maven idea plugin is retired, and not really useful. Better to let IDEA generate the idea files based on the pom.xml. maven.apache.org/plugins/maven-idea-plugin
  • user1485864
    user1485864 almost 4 years
    And for Scala / sbt projects: Try View -> Tool Windows -> Sbt projects, then right click on the project and (Reimport sbt project). If this does not work, in the sbt console do manual clean then update and then compile.