2

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.

KasparTr
  • 2,328
  • 5
  • 26
  • 55
  • It may be but I have the exact same problem with Maven project when I add the org.junit junit5-api 5.0.0-ALPHA To pom.xml – KasparTr Sep 10 '17 at 12:40

1 Answers1

2

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'  } 
}
Naman
  • 27,789
  • 26
  • 218
  • 353
  • I did exactly this. IDE even asked my to add the jUnit5 dependency to classpath which I accepted. After that it imported org.junit.jupiter.api.Test; @ParameterizedTest still is not resolved. Is it the annotation maybe? – KasparTr Sep 10 '17 at 09:55
  • @KasparTr do you have `import org.junit.jupiter.params.ParameterizedTest` in your class? If yes, what error do you get? – Naman Sep 10 '17 at 09:57
  • the error is: Cannot resolve symbol 'params' for the import statement. – KasparTr Sep 10 '17 at 10:00
  • @KasparTr Seems like the junit5 dependencies are not synced in your project. are you able to annotate `org.junit.jupiter.api.Test`? – Naman Sep 10 '17 at 10:20
  • @KasparTr If the jupiter `@Test` works while the other doesn't. I would suggest cleaning your jars. Delete them once and sync again to avoid any corrupted jar. Also you can traverse the package to find the class by yourself if its fetched on classpath. – Naman Sep 10 '17 at 10:26
  • idk what you mean by clening jars but I deleted all classpath dependencies and refreshed the buid.gradle file. In IDEA there are no jUnit jars in the "External Libraries" anyway. The problem is the same still after re-adding the classpaths (the second time I also selected the "Copy junit5 libraries to..." instead of using the IntelliJ IDEA distributor). Still exactly the same problem. – KasparTr Sep 10 '17 at 11:03
  • *IDEA there are no jUnit jars in the "External Libraries" anyway* seems to be the cause of your problem. Ideally, all jars are fetched by intelliJ under external jars. Is there a reproducible sample to this? – Naman Sep 10 '17 at 11:05
  • thanks for the help, please see edit in question if you have time – KasparTr Sep 10 '17 at 11:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154064/discussion-between-nullpointer-and-kaspartr). – Naman Sep 10 '17 at 11:54
  • The link is broken. I believe it should redirect to eg. https://github.com/junit-team/junit5-samples/blob/master/junit5-jupiter-starter-gradle/build.gradle Please, verify it. – mmBs Apr 07 '19 at 12:43