簡體   English   中英

右鍵單擊文件夾時 Intellij 中出現“No junit.jar”錯誤,但在運行單個測試類時不會出現

[英]"No junit.jar" error in Intellij when right clicking a folder, but not when running individual classes of tests

我的 Spring 引導應用程序有幾個測試類。

當右鍵單擊測試 class 並選擇Run XYZtest選項時,它可以正常工作。

But when selecting the folder that contains that class and selecting Run test in com.example.my I get an error of No junit.jar:

在此處輸入圖像描述

從 mvn 運行所有測試時,它們運行沒有問題。

根據一些建議,我在Library部分專門添加了junit.jar ,但沒有幫助:

"

Spring boot is using JUnit 5. 如果你使用 spring boot initializr 網站,你應該有這樣的依賴。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

您在測試中使用了 JUnit 4 還是 5? 您可以嘗試更新 IntelliJ 或 JUnit 插件,這可能會解決您的問題。

我遇到了這個問題,這就是我解決它的方式:

語境:

  • SpringBoot 應用程序
  • 使用maven管理多個模塊
  • 在根 POM 的dependencyManagement中添加junit的 maven 依賴(而不是dependencies ,它們的區別可以在這里找到)
  • 打算測試 class 或根模塊的子模塊之一內的文件夾

PS:如果您的情況與以上上下文不符,此方案可能無法解決您的問題。

腳步

  1. 右鍵單擊 class 或要測試的文件夾:
    在此處輸入圖像描述
  2. 選擇More Run/Debug -> Modify Run Configuration
  3. 將模塊選項更改為要從根模塊測試的選項在此處輸入圖像描述

我設法通過在根 pom.xml 文件中添加 Junit Maven 依賴關系來解決該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM