简体   繁体   中英

Including JUnit 5 dependency in IntelliJ IDEA

From jetbrains blog :

IntelliJ IDEA supports the ability to actually run tests written for JUnit 5 – there's no need to use the additional libraries (like the Gradle or Maven plugins for example), all you need is to include the JUnit 5 dependency.

I'm new to Java and IntelliJ IDEA and it's not clear to me what are the steps that I should do for making test using Junit 5.

If your project is Maven or Gradle based, the dependency is added via pom.xml or build.gradle , otherwise you just add the .jar files to the Module Dependencies .

IDE can help you with that, press Alt + Enter on the red code:

添加库

The following dependencies will be downloaded from the Maven repository and added to the classpath:

DEPS

I made this work by adding this to my pom:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.0.0-M4</version>
    <scope>test</scope>
</dependency>       
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.0.0-M4</version>
    <scope>test</scope>
</dependency>

Previously you need plugin to run unit test like this

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-M2'
    }
}

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

But for JUnit5 no need of plugin just compile

dependencies {
     testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M2'
}

Got the latest IntelliJ IDEA (2017.3), being able to add JUnit5 library when creating test class in IntelliJ, but still failed to find tests. Tried the suggestion by @CrazyCoder, and found out the org.junit.jupiter.api has existed in my IntelliJ and has the version 5.0.0-M6. And finally solved by downloading org.junit.platform:junit-platform-commons:1.0.0-M6 from Maven Repository and adding it into classpath.

For someone like me, new to the IntelliJ, the detailed steps I followed:

  1. Open Project Settings -> Libraries -> + New Project Library -> from Maven...
  2. Search and add org.junit.platform:junit-platform-commons:1.0.0-M6
  3. Modules -> module name you want to add it to -> Dependencies -> + 2 Library ... (should have library jar listed)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM