2
votes

Running the IDEA IDE I want to add a gradle dependency for the jUnit v5.

Here is my build.gradle file, I used this answer as a guide.

apply plugin: 'java'
sourceCompatibility = 1.8

repositories { mavenCentral() }
apply plugin: 'org.junit.platform.gradle.plugin'

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'junit:junit:4.12'

    testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")

    testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M4")

    // Enable use of the JUnitPlatform Runner within the IDE
    testCompile("org.junit.platform:junit-platform-runner:1.0.0-M4")
    compile ("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}

junitPlatform {
    details 'tree'
}

The problem here is that the jUnit4 annotations are resolved by import but all the v5 annotations are not resolved.

One example:

@ParameterizedTest
public void testExample() {
    // My annotations is not resolved
}

What is the right way to add a jUnit5 dependency using gradle?

EDIT I started a new gradle java project from scratch to get to the bottom of this.

Here is my current build.gradle.

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

apply plugin: 'java'
sourceCompatibility = 1.8

repositories { mavenCentral() }
apply plugin: 'org.junit.platform.gradle.plugin'

dependencies {
    testCompile group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '4.12.0-RC3'

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-RC3'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-RC3'
    compile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.0-RC3'

    testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-RC3'
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}

junitPlatform {
    details 'tree'
}

I wrote the @Test annotation in a new class file under test after which it asked me to "add the 'jUnit5' to classpath

enter image description here

which I did and this time selected the Copy 'jUnit5' libraries to... instead of using the IDEA distributor.

Now it added these files in module:

enter image description here

The file is still RC2 but in build.gradle it is RC3. There are also no jUnit jars in "External Library" directory

What am I missing, the problem is still that the IDE cannot resolve some jUnit5 annotations like @ParamiterizedTest.

1
It may be but I have the exact same problem with Maven project when I add the <dependency> <groupId>org.junit</groupId> <artifactId>junit5-api</artifactId> <version>5.0.0-ALPHA</version> </dependency> To pom.xmlKasparTr

1 Answers

2
votes

Here is a quick sample on how to configure gradle with junit5. In your dependencies, remove the compile statement for the junit:4.12 artifact verison.

// If you also want to support JUnit 3 and JUnit 4 tests
testCompile("junit:junit:4.12")

In the buildscript() method, include these:

buildscript {   
  repositories { mavenCentral() }
  dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3'  } 
}