【发布时间】:2018-07-28 03:27:09
【问题描述】:
我尝试在我的 INtelliJ IDEA Community Edition 2018.2 上设置 JUnit 5。 jar 已下载,但我在导入时收到 Cannot resolve symbol Assertions
导入静态 org.junit.jupiter.api.Assertions.*;
【问题讨论】:
标签: intellij-idea junit
我尝试在我的 INtelliJ IDEA Community Edition 2018.2 上设置 JUnit 5。 jar 已下载,但我在导入时收到 Cannot resolve symbol Assertions
导入静态 org.junit.jupiter.api.Assertions.*;
【问题讨论】:
标签: intellij-idea junit
您是否尝试在常规应用类而不是测试类中使用 JUnit 断言?
<scope>test</scope>
当 Maven 依赖项带有一个值为 test 的 scope 元素时,这意味着您不能在特定于测试的源包/文件夹之外使用该库。
如果您尝试从示例项目的 src/main/java/… 文件夹层次结构中的代码调用 JUnit,您将看到该错误。如果你从src/test/java… 调用JUnit,你会看到成功。
要在 src/main/java/… 文件夹层次结构中启用 JUnit,请删除 POM 依赖项中的 scope 元素。所以这个:
<!-- 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>
…变成这样:
<!-- 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>
</dependency>
顺便提一下,从 JUnit 5.4.0 开始,我们可以指定 junit-jupiter 的新的非常方便的单个 Maven 工件,这反过来将为您的项目提供 8 个库。
【讨论】: