【问题标题】:import junit jupiter api not found找不到导入junit jupiter api
【发布时间】:2017-12-23 19:57:20
【问题描述】:

我正在使用 Junit 在 Netbeans 的 Maven 项目中测试一些代码。指定的依赖项是 Junit 4.12(在 pom.xml 中)。

但是,我在尝试构建时遇到编译器错误:

error: package org.junit.jupiter.api does not exist

在这一行: import org.junit.jupiter.api.Test;

我怀疑这是 Junit4/Junit5 的事情,因为当我在 IntelliJ 中打开旧版本的项目时,它会将 Junit5 列为依赖项。我应该只使用Junit5吗?

任何解决构建错误的帮助将不胜感激!

【问题讨论】:

    标签: java maven netbeans junit


    【解决方案1】:

    您需要在开始编写测试之前注入jupiter artefact

    Group ID: org.junit.jupiter
    Artifact ID: junit-jupiter-api
    Version: 5.0.0-M5
    

    JUnit JupiterJUnit 5 的子模块,所以你需要使用JUnit 5

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.0.0-M5</version>
        <scope>test</scope>
    </dependency>
    

    【讨论】:

    • 谢谢,这是我目前拥有的:&lt;dependency&gt; &lt;groupId&gt;junit&lt;/groupId&gt; &lt;artifactId&gt;junit&lt;/artifactId&gt; &lt;version&gt;4.12&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt;
    • 但是,我仍然遇到与以前相同的错误
    • 对不起,我的意思是我改成你说的了
    • 为什么这个依赖需要单独包含?为什么junit依赖不够?
    【解决方案2】:

    我认为您的问题与&lt;scope&gt;test&lt;/scope&gt;有关

    您的测试类应该在测试包中。

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-platform.version}</version>
            <scope>test</scope>
        </dependency>
    

    你的测试应该在这里

    【讨论】:

      【解决方案3】:

      在我的情况下,它通过在应用程序中添加这一行来工作:build.gradle

      dependencies {  
      implementation 'org.junit.jupiter:junit-jupiter-api:5.5.1'
      }
      

      【讨论】:

      • “无法解决:org.junit.jupiter:junit-jupiter”:-/
      猜你喜欢
      • 2018-07-05
      • 2021-05-30
      • 1970-01-01
      • 2021-08-05
      • 2020-07-19
      • 2021-10-09
      • 2020-10-09
      • 2019-06-08
      • 1970-01-01
      相关资源
      最近更新 更多