【问题标题】:"No junit.jar" error in Intellij when right clicking a folder, but not when running individual classes of tests右键单击文件夹时 Intellij 中出现“No junit.jar”错误,但在运行单个测试类时不会出现
【发布时间】:2020-08-06 11:14:44
【问题描述】:

我的 Spring Boot 应用程序有几个测试类。

当右键单击一个测试类并选择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,但没有帮助:

【问题讨论】:

  • Try Invalidate Cache and Restart File > Invalidate Caches/Restart... 另外,尝试重新加载 maven 项目。
  • @rimonmostafiz 是的。我试过了,但没有用。
  • 确保你有正确版本的junit库添加到你有junit测试的每个模块的依赖项中。如果是基于 Maven 的项目 - 检查每个模块的 pom.xml 文件中是否存在 junit 依赖项。

标签: java intellij-idea junit


【解决方案1】:

Spring boot 使用的是 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 插件,这可能会解决您的问题。

【讨论】:

    【解决方案2】:

    我遇到了这个问题,我就是这样解决的:

    上下文:

    • SpringBoot 应用程序
    • 使用 maven 管理多个模块
    • 在根POM的dependencyManagement中添加junit的maven依赖(而不是dependencies,他们的区别可以在here找到)
    • 打算在根模块的子模块之一中测试类或文件夹

    PS:如果您的情况与以上上下文不符,此方案可能无法解决您的问题。

    步骤

    1. 右键单击要测试的类或文件夹:
    2. 选择More Run/Debug -> Modify Run Configuration
    3. 从根模块将模块选项更改为您要测试的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2016-08-29
      • 1970-01-01
      相关资源
      最近更新 更多