Gradle project running jUnit 5 tests in IntelliJ

Jean-François Beauchef

I am trying both Gradle and jUnit5 right now. Everything works fine except that I cannot run a specific jUnit test. The "Run 'SampleTest'" option does not appear when I right-click a test class.

I have the latest version of IntelliJ (2016.1.3) Ultimate. Here is my build.gradle file:

repositories {
    mavenCentral()
}

apply plugin: 'java'

version = '1.0.0-SNAPSHOT'

jar {
    baseName = 'test-project'
}

dependencies {
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M1'
}

The project structure is the standard one (like in Maven). And here is an example of a test:

package com.test;

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

public class SampleTest {
  @Test public void sampleTest() {
    int test = 1;
    Assertions.assertTrue(test == 1);
  }
}

What am I missing?

EDIT:

It seems that Gradle is not picking up my test either. When I go to build/reports/tests/index.html, it indicates 0 test.

FINAL EDIT:

Following @dunny's answer, here is what I did to make everything work. I modified my build.gradle file like this:

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

repositories {
    mavenCentral()
}

apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

version = '1.0.0-SNAPSHOT'

jar {
    baseName = 'test-project'
}

dependencies {
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M1'
    testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-M1'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M1'
}

test {
    testLogging {
        events 'started', 'passed'
    }
}

In IntelliJ, I then opened the Gradle window, and clicked on the "refresh all gradle projects" button, to refresh the libraries.

Then in my test class, I added @RunWith(JUnitPlatform.class) on top of the class declaration.

And when I do a gradle build, the results are available here: build\test-results\junit-platform\TEST-junit-jupiter.xml

dunni

IntelliJ 2016.1.3 doesn't have support for JUnit 5 tests. You can however add the annotation @RunWith(JUnitPlatform.class), which would execute your test in a JUnit 4 compatibility mode (you can still use all JUnit 5 features). See http://junit.org/junit5/docs/current/user-guide/#running-tests-junit-platform-runner for more information.

For Gradle you need to include the Gradle JUnit 5 plugin to enable support:

buildscript {
    repositories {
        mavenCentral()
        // The following is only necessary if you want to use SNAPSHOT releases.
        // maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M1'
    }
}

apply plugin: 'org.junit.platform.gradle.plugin'

See http://junit.org/junit5/docs/current/user-guide/#running-tests-build

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Gradle project running jUnit 5 tests in IntelliJ

From Java

Maven not running JUnit 5 tests

From Dev

Junit5 with IntelliJ and Gradle

From Java

Running JavaFX project in IntelliJ with Gradle

From Dev

Running JavaFX project in IntelliJ with Gradle

From Dev

JHipster & Gradle: Running Junit tests from STS

From Dev

JUnit 5 tests are not recognizable in IntelliJ Idea

From Java

"No tests found for given includes" when running Gradle tests in IntelliJ IDEA

From Dev

"No tests found for given includes" when running Gradle tests in IntelliJ IDEA

From Dev

Set up environment variables for Gradle Runner and JUnit tests on Intellij Idea

From Dev

"Address already in use: bind" exception when running Gradle JUnit tests

From Dev

Maven running tests annotated with junit @Ignore when building Java project

From Dev

Maven running tests annotated with junit @Ignore when building Java project

From Dev

All tests get terminated in Intellij with Spring Boot and JUnit5

From Dev

IntelliJ: Getting Spring+Gradle+JUnit project to work within IDE

From Dev

Intellij Gradle project cannot resolve assertEquals with junit 4.11 as the testCompile dep

From Dev

IntelliJ: Getting Spring+Gradle+JUnit project to work within IDE

From Dev

Running AsyncHttpClient with JUNIT Tests

From Dev

Running AsyncHttpClient with JUNIT Tests

From Dev

How to pass gradle systemProperties JUnit5 tests?

From Dev

Running specific set-up for tagged tests in JUnit 5

From Dev

Dagger 2 Annotation Processor Not Running for Java Gradle IntelliJ Project

From Dev

Gradle running tests sequentially

From Dev

Gradle not running tests

From Dev

Gradle file is not running tests

From Dev

Profiling junit tests with intellij and jprofiler

From Java

How to run JUnit tests with Gradle?

From Dev

Running JUnit tests from Terminal

From Dev

Skip tests in IntelliJ using Gradle

Related Related

  1. 1

    Gradle project running jUnit 5 tests in IntelliJ

  2. 2

    Maven not running JUnit 5 tests

  3. 3

    Junit5 with IntelliJ and Gradle

  4. 4

    Running JavaFX project in IntelliJ with Gradle

  5. 5

    Running JavaFX project in IntelliJ with Gradle

  6. 6

    JHipster & Gradle: Running Junit tests from STS

  7. 7

    JUnit 5 tests are not recognizable in IntelliJ Idea

  8. 8

    "No tests found for given includes" when running Gradle tests in IntelliJ IDEA

  9. 9

    "No tests found for given includes" when running Gradle tests in IntelliJ IDEA

  10. 10

    Set up environment variables for Gradle Runner and JUnit tests on Intellij Idea

  11. 11

    "Address already in use: bind" exception when running Gradle JUnit tests

  12. 12

    Maven running tests annotated with junit @Ignore when building Java project

  13. 13

    Maven running tests annotated with junit @Ignore when building Java project

  14. 14

    All tests get terminated in Intellij with Spring Boot and JUnit5

  15. 15

    IntelliJ: Getting Spring+Gradle+JUnit project to work within IDE

  16. 16

    Intellij Gradle project cannot resolve assertEquals with junit 4.11 as the testCompile dep

  17. 17

    IntelliJ: Getting Spring+Gradle+JUnit project to work within IDE

  18. 18

    Running AsyncHttpClient with JUNIT Tests

  19. 19

    Running AsyncHttpClient with JUNIT Tests

  20. 20

    How to pass gradle systemProperties JUnit5 tests?

  21. 21

    Running specific set-up for tagged tests in JUnit 5

  22. 22

    Dagger 2 Annotation Processor Not Running for Java Gradle IntelliJ Project

  23. 23

    Gradle running tests sequentially

  24. 24

    Gradle not running tests

  25. 25

    Gradle file is not running tests

  26. 26

    Profiling junit tests with intellij and jprofiler

  27. 27

    How to run JUnit tests with Gradle?

  28. 28

    Running JUnit tests from Terminal

  29. 29

    Skip tests in IntelliJ using Gradle

HotTag

Archive