【问题标题】:Including JUnit 5 dependency in IntelliJ IDEA在 IntelliJ IDEA 中包含 JUnit 5 依赖项
【发布时间】:2017-07-31 23:23:05
【问题描述】:

来自jetbrains blog

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

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

【问题讨论】:

    标签: java intellij-idea junit junit5


    【解决方案1】:

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

    IDE 可以帮你解决这个问题,在红色代码上按 Alt+Enter

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

    【讨论】:

    • 您好,感谢您的帮助,我完全是 Java 初学者,所以 Maven 和 Gradle 对我来说并不熟悉。我需要下载哪些 jar 文件,从哪里下载,我应该如何处理它们? (IDE 只允许我使用 alt + enter 添加 JUnit 4)。
    • jetbrains.com/idea/download/index.html尝试最新的IDE版本。
    【解决方案2】:

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

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

    【讨论】:

    • 我在哪里写依赖?
    • 写在你的app模块的build.gradle中
    【解决方案3】:

    得到最新的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 并将其添加到类路径中来解决。

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2014-02-10
        • 1970-01-01
        • 2018-05-07
        • 1970-01-01
        • 2012-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多