Intellij "java: package org.junit does not exist"
Asked Answered
H

14

44

When I try to run my test I get the following Errors:

Error:(3, 24) java: package org.junit does not exist
Error:(3, 1) java: static import only from classes and interfaces
Error:(5, 17) java: package org.junit does not exist
Error:(6, 17) java: package org.junit does not exist
Error:(12, 10) java: cannot find symbol
    symbol:   class Before
    location: class edu.kit.ipd.swt1.SimpleEditMeTest
Error:(17, 10) java: cannot find symbol
    symbol:   class Test
    location: class edu.kit.ipd.swt1.SimpleEditMeTest
[...]

My test code:

package edu.kit.ipd.swt1;
    
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Test;

public class SimpleEditMeTest {
    private EditMe editMe;
    
    @Before
    public void setUp() throws Exception {
        editMe = new EditMe();
    }

    @Test
    public void test() {
        assertNotNull(editMe.getFoo());
    }
}

Screenshot of the whole project

Run configurations

Dependencies i.stack.imgur. com/OiQWU.png (Can't post more than 2 links)

Hygienist answered 20/4, 2015 at 7:34 Comment(1)
Is junit declared as a test dependency in your pom.xml file?Jabez
S
37

This worked for me:

  1. Right click pom.xml
  2. Select Maven
  3. Reload project
Sansen answered 24/11, 2020 at 3:57 Comment(1)
Thanks! I had to close the project and reopen, IntelliJ synched all the dependences in the pom file.Erlin
P
23

You are trying to run your test so you need to set the scope of JUnit to "compile" same as in the picture:

1

Plumbo answered 7/5, 2021 at 7:18 Comment(1)
What's the point of the Test scope if this is necessary?Fabi
H
18

I had the same problem. In order to fix it I had to Open Module Settings for my project and manually add jar Dependencies junit-4.12.jar and hamcrest-core-1.3.jar which are contained in the IntelliJ installation lib directory.

Hexagon answered 17/5, 2017 at 12:50 Comment(4)
I get Failed to load plugin descriptorHower
This failed for me I got "duplicate source error". My fix was to delete the duplicate source folder, see here https://mcmap.net/q/375089/-when-adding-jar-module-quot-android-quot-must-not-contain-source-rootMarquise
If you can run tests from IntelliJ but org.junit.runner can't be found. This solution solves it too. Thanks!Alcheringa
Dont do it like this, please remove this answer. It only works on your local machine, but when you work with others on the same project it will not be fixed.Tager
T
16

For anyone ending up here using maven, a couple of things to check:

  • (Sanity check) Have you added junit as a dependency in your pom.xml?
  • Have Intellij accepted your module as a maven module? Try Right clicking the module/project and select Add as Maven Project or perhaps Maven->Reimport
  • Are you using explicit version in your dependency declaration in your pom file? Like <version>4.12</version>. Try that. (You might not want to this because you want the version to be decided by a parent module. Read on.)
  • Have you remembered to add a dependencyManagement? Maven need it to resolve the correct version number if you don't want to have it explicitly in the current project. For me it refers to the parent project. Like this:
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.myorg</groupId>
            <artifactId>myproject-parent</artifactId>
            <version>${myproject.version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>
Totter answered 30/3, 2018 at 15:11 Comment(2)
Maven->Reimport and then Build->Rebuild fixed it for me.Phiz
Gaaaaaaaaaa, the simple things. Yes, Going to the Maven dashboard (in intellij) and clicking re-import cleared the error. There are several ways to do a dependency re-import for future readers.Collagen
B
6

The solution to this is to add junit library as dependency to the project. This can be done by adding junit to global library and then hovering over the error(junit word) and right clicking to add junit to class path. alt+shift+ctrl+s to get project settings in the same either add junit to global library or to project section as mentioned by rob in his answer. once you add it here the red bulb icon would give you option to resolve the error by adding it to class path.

Burdelle answered 19/2, 2020 at 19:54 Comment(1)
This fixed it for me, but I am curious why this is necessary. Why is it in pom.xml and do I need to add it as a global library too?Leasehold
M
2

Adding the JUnit dependency to build.gradle solved the issue

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'junit:junit:4.12'
}
Mani answered 17/2, 2021 at 17:44 Comment(0)
P
1

I had the same problem. Adding JARs made no difference. I solved it by adding library (not JAR) dependency for Junit 5.3

Patina answered 18/3, 2019 at 14:58 Comment(0)
D
1

You can go to project structure and edit dependencies by changing the junit dependency to compile from just test

intelij community edition

Diphyllous answered 12/2, 2021 at 11:33 Comment(0)
A
1

For my case, I have also added junit library dependency manually to the project.

I'm using IntelliJ 2020.2

And it works !

capture image adding junit library to intellij

Abampere answered 10/3, 2021 at 9:39 Comment(0)
V
1

I see this as a bug from intellij as they are not able to sometimes load the dependencies with the test scope during the build operations. You can see that this error will only come for dependencies with the test scope.

Vainglory answered 18/6, 2021 at 10:38 Comment(0)
P
1

I had this similar problem that gave me headaches. Then I noticed that I had some duplicates and the src/test folder added for Test Source Folders in Project Structure > Project Settings > Modules (only src/test/java should be added) :

Modules sources roots

So check your Modules sources roots.

Prevot answered 7/8, 2023 at 16:16 Comment(1)
This really helped. I was also getting compilation errors for the code in src/main/java. I tried the same suggestion for Test Source folder as well as the Source folder. Keeping only src/test/java in test source folders and src/main/java in source folder resolved both the compilation errors and junit jupter package does not exist error. Thank you.Catatonia
T
1

I found this question while googling as I encountered this error when importing a legacy Gradle Java app into IntelliJ. None of the other answers solved my issue.

What I found was that IntelliJ was trying to import the src/main/java and src/test/java folders as modules in the Project Wizard. When I deleted the recreated the IntelliJ project and explicitly un-clicked the recommended modules, the Gradle build worked successfully on the second run, since the first build had a separate transient error.

Tartaric answered 14/8, 2023 at 22:46 Comment(0)
P
0

If you are using Jupiter with Junit5 testing, Then you need 2 things: -> Jupiter-junit-api -> Jupiter Engine

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-api</artifactId>
  <version>5.7.1</version>
  <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <version>5.7.1</version>
  <scope>test</scope>
</dependency>
Phile answered 24/4, 2021 at 11:0 Comment(0)
I
0

I deleted the test folder inside src folder

Intro answered 17/5, 2024 at 16:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.