Search code examples
javaintellij-ideajunit

JUnit not recognized in IntelliJ


I am trying to integrate and setup the JUnit 5 libraries with IntelliJ on my linux distro. I added JUnit to my gradle, and built it using gradle. But I am still seeing an error on my unit tests prompting me to "add junit to my classpath" even though it already is. enter image description here

Here is my build.gradle

    id 'java'
    id 'idea'
}

group 'com.techchallenge'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
    testCompile('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}
test {
    useJUnitPlatform()
}

UPDATE See screenshot. I added the dependency and it is still prompting me to add the junit to my classpath though I already did updated intellij

UPDATE 2 I updated the build.gradle and also did gradlew clean build test which was successful. But it is still showing the error with my junit annotations- and keeps prompting me to add junit 5.4 to my classpath though it is there.

    id 'java'
    id 'idea'
}

group 'com.techchallenge'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    implementation 'org.junit.jupiter:junit-jupiter:5.4.2'

    testCompile('org.junit.jupiter:junit-jupiter-api:5.4.2')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2')
    testRuntime('org.junit.vintage:junit-vintage-engine:5.4.2')

}
test {
    useJUnitPlatform()
}

errors on intellij


Solution

  • I had the same problem. After fixing the gradle build as shown in the jupiter example project (https://junit.org/junit5/docs/current/user-guide/#overview-getting-started-example-projects), it still did not work. Cleaning the caches and restarting the IDE did also not work.

    What fixed it for me was, I closed the IDE, deleted the .idea and .gradle folders in the project and started the IDE again.