Linkx_lair
Linkx_lair

Reputation: 663

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

Upvotes: 2

Views: 6318

Answers (4)

sweintritt
sweintritt

Reputation: 564

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.

Upvotes: 2

gil.fernandes
gil.fernandes

Reputation: 14641

I had a similar problem with JUnit 5 using Maven. I discovered that IDEA had created some *.iml files where it should not have. I had one in the test folder called test.iml. After deleting them the problems went away.

Please check if IDEA did not create some sneaky modules unexpectedly.

I see in the screenshot that you have one of those:

enter image description here

My advice is to delete it.

Upvotes: 6

eHayik
eHayik

Reputation: 3262

It should recognise your setup. You may try adding junit5 dependencies (which is how junit5 is added creating a Gradle project from scratch using IntelliJ's New Project option) as below then reload your Gradle Project.

    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
    } 

You may try File | Invalidate caches/Restart... option to get rid of odd issues if your project runs tests successfully using gradle test cli command

https://www.jetbrains.com/help/idea/invalidate-caches.html

You have to use the right imports for Junit 5, You are using Junit4 imports. Since version 5 @Test, Assertions, etc are within org.junit.jupiter.api package. e.g:

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class DummyTest {

    @BeforeEach
    void setUp() {

    }

    @AfterEach
    void tearDown() {

    }

    @Test
    void name() {
        Assertions.assertTrue(true);
    }

You may look into junit5-jupiter-starter-gradle

Upvotes: 1

SebastianX
SebastianX

Reputation: 219

Make sure you have correctly setup gradle inside your intellij like for maven

and make sure You have installed JUnit plugin for IntelliJ IDEA,

enter image description here

you can even point gradle to your local repository like

repositories {
  flatDir {
   dirs 'C:/path/to/local/directory'
  }
}
dependencies {
compile name: 'name-of-jar'
}

Upvotes: -1

Related Questions