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
import junit jupiter api not found
提问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 jupiter
artefact before start writing Tests
jupiter
在开始编写测试之前,您需要注入人工制品
Group ID: org.junit.jupiter
Artifact ID: junit-jupiter-api
Version: 5.0.0-M5
JUnit Jupiter
is submodule of JUnit 5
so 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
你的测试应该在这里
回答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'
}