繁体   English   中英

在IntelliJ IDEA中包含JUnit 5依赖项

[英]Including JUnit 5 dependency in IntelliJ IDEA

来自jetbrains博客

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

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

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

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

添加库

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

DEPS

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

以前你需要插件来运行这样的单元测试

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'

但是对于JUnit5,不需要插件就可以编译

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

获得了最新的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并将其添加到classpath中来解决。

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

  1. 从Maven打开项目设置 - >库 - > +新项目库 - >
  2. 搜索并添加org.junit.platform:junit-platform-commons:1.0.0-M6
  3. 模块 - > 要将其添加到的模块名称 - >依赖项 - > + 2库... (应该列出库jar)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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