簡體   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