java 未找到导入 junit jupiter api

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45175418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:33:48  来源:igfitidea点击:

import junit jupiter api not found

javamavennetbeansjunit

提问by information_interchange

I am using Junit to test some code in a Maven project in Netbeans. The dependency specified is Junit 4.12 (in the pom.xml).

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

However, I am getting a compiler error when I try and build:

但是,当我尝试构建时出现编译器错误:

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

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

on this line: import org.junit.jupiter.api.Test;

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

I suspect it is a Junit4/Junit5 thing, since when I open an older version of the project in IntelliJ, it lists Junit5 as a dependency. Should I just use Junit5?

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

Any help in resolving the build error would be appreciated!

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

回答by Aryan

You need to Inject jupiterartefact before start writing Tests

jupiter在开始编写测试之前,您需要注入人工制品

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

JUnit Jupiteris submodule of JUnit 5so you need to use JUnit 5

JUnit Jupiter是子模块,JUnit 5所以你需要使用 JUnit 5

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

回答by Ahmed Aziz

I think your problem related to the <scope>test</scope>

我认为你的问题与 <scope>test</scope>

Your test class should be in the test package.

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

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

Your test should be here

你的测试应该在这里

enter image description here

在此处输入图片说明

回答by Rahul Khatri

In my case it worked by adding this line in app: build.gradle

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

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