Java 在 IntelliJ IDEA 中包含 JUnit 5 依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42721368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:19:55  来源:igfitidea点击:

Including JUnit 5 dependency in IntelliJ IDEA

javaintellij-ideajunitjunit5

提问by Stav Alfi

From jetbrains blog:

来自jetbrains 博客

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.

IntelliJ IDEA 支持实际运行为 JUnit 5 编写的测试的能力——无需使用额外的库(例如 Gradle 或 Maven 插件),您只需要包含 JUnit 5 依赖项。

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.

我是 Java 和 IntelliJ IDEA 的新手,我不清楚使用 Junit 5 进行测试应该执行哪些步骤。

采纳答案by CrazyCoder

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

如果您的项目基于 Maven 或 Gradle,则通过pom.xml或添加依赖项build.gradle,否则您只需将.jar文件添加到Module Dependencies

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

IDE 可以帮助您,在红色代码上按Alt+ Enter

add library

添加库

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

以下依赖项将从 Maven 存储库中下载并添加到类路径中:

deps

深度

回答by Aryan

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

但是对于 JUnit5 不需要插件只需编译

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

回答by nahs

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-M6from Maven Repository and adding it into classpath.

拿到了最新的IntelliJ IDEA(2017.3),在IntelliJ中创建测试类的时候可以添加JUnit5库,但是还是找不到测试。尝试了@CrazyCoder 的建议,发现 org.junit.jupiter.api 已存在于我的 IntelliJ 中,并且版本为 5.0.0-M6。最后通过从 Maven Repository下载 org.junit.platform:junit-platform-commons:1.0.0-M6并将其添加到类路径中来解决。

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

对于像我这样的 IntelliJ 新手,我遵循的详细步骤:

  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)
  1. 打开项目设置 -> 库 -> + 新建项目库 -> 从 Maven...
  2. 搜索并添加org.junit.platform:junit-platform-commons:1.0.0-M6
  3. Modules ->要添加的模块名称-> Dependencies -> + 2 Library ... (应该列出库 jar)

回答by Qasim Ali

I made this work by adding this to my pom:

我通过将它添加到我的 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>