[英]JUnit5: Trouble Importing Assertions
我正在嘗試使用 JUnit5 創建一些基本的單元測試。 我轉到我的Analyzer.java
類並獲取用於創建測試的彈出窗口。 我點擊Create New Test
,將測試庫設置為 JUnit5。 我檢查了一堆方法來生成測試方法並點擊確定。
所以現在我有一個AnalyzerTest.java
文件,在頂部我有:
import static org.junit.jupiter.api.Assertions.*;
不幸的是, Assertions
是紅色的(這是在 IntelliJ IDEA 中)。 當我懸停時,它說“找不到符號斷言”。 同樣,我有:
@org.junit.jupiter.api.Test
在每個測試方法之前以及當我懸停時,我得到“無法解析符號測試”
我只是想創建然后運行一些單元測試,但顯然我做錯了什么。
有任何想法嗎?
謝謝!
將以下依賴項添加到您的 Gradle:
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.1")
在您的依賴項下。
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.1")
我不知道你在使用 Maven 還是 gradle。 但是,如果您使用的是 maven,只需在標簽之間添加以下依賴項。 如果您在這方面需要更多幫助,請告訴我。 我在下面使用了舊版本,您可以從https://mvnrepository.com查看最新版本並更新 pom 腳本。
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
如果您使用 IntelliJ-IDEA,請確保您的測試文件位於測試源根目錄中。
因為我的項目沒有路徑src/test/java
,當我使用Ctrl + Shift + T添加測試文件時,它添加到src/main/java
...
如果使用 Maven,請確保在dependency
元素內指定一個dependencies
元素。
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.0-RC1</version>
<scope>test</scope>
</dependency>
如果您想在與測試相關的類之外使用這些斷言,請在常規應用程序類中刪除<scope>test</scope>
元素。
請注意,從 JUnit 5.4.0 開始,我們可以指定junitjupiter
的新單個 Maven 工件, junitjupiter
為您的項目提供 8 個庫。 如果您只編寫 JUnit 5 測試(Jupiter 測試引擎),而不是“老式”JUnit 4 測試或其他測試引擎,則非常方便。
如果使用 Gradle 而不是 Maven,請參閱RileyManda的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.