[英]JUnit5: Trouble Importing Assertions
我正在尝试使用 JUnit5 创建一些基本的单元测试。 我转到我的Analyzer.java
类并获取用于创建测试的弹出窗口。 我点击Create New Test
,将测试库设置为 JUnit5。 我检查了一堆方法来生成测试方法并点击确定。
所以现在我有一个AnalyzerTest.java
文件,在顶部我有:
import static org.junit.jupiter.api.Assertions.*;
不幸的是, Assertions
是红色的(这是在 IntelliJ IDEA 中)。 当我悬停时,它说“找不到符号断言”。 同样,我有:
@org.junit.jupiter.api.Test
在每个测试方法之前以及当我悬停时,我得到“无法解析符号测试”
我只是想创建然后运行一些单元测试,但显然我做错了什么。
有任何想法吗?
谢谢!
将以下依赖项添加到您的 Gradle:
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.1")
在您的依赖项下。
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.1")
我不知道你在使用 Maven 还是 gradle。 但是,如果您使用的是 maven,只需在标签之间添加以下依赖项。 如果您在这方面需要更多帮助,请告诉我。 我在下面使用了旧版本,您可以从https://mvnrepository.com查看最新版本并更新 pom 脚本。
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
如果您使用 IntelliJ-IDEA,请确保您的测试文件位于测试源根目录中。
因为我的项目没有路径src/test/java
,当我使用Ctrl + Shift + T添加测试文件时,它添加到src/main/java
...
如果使用 Maven,请确保在dependency
元素内指定一个dependencies
元素。
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.0-RC1</version>
<scope>test</scope>
</dependency>
如果您想在与测试相关的类之外使用这些断言,请在常规应用程序类中删除<scope>test</scope>
元素。
请注意,从 JUnit 5.4.0 开始,我们可以指定junitjupiter
的新单个 Maven 工件, junitjupiter
为您的项目提供 8 个库。 如果您只编写 JUnit 5 测试(Jupiter 测试引擎),而不是“老式”JUnit 4 测试或其他测试引擎,则非常方便。
如果使用 Gradle 而不是 Maven,请参阅RileyManda的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.