[英]JUnit5: Trouble Finding AssertEquals
我已经在 IntelliJ IDEA 中设置了 JUnit,并进行了一堆没有任何内容的测试。 当我运行它们时,它们都按预期通过。 但是,当我输入“assertEquals”时,它显示为红色。 当我将鼠标悬停在它上面时,它会显示“无法解析方法”。
我用谷歌搜索了一下,看起来我需要这样做:
import static org.junit.Assert.*;
但是,当我开始输入import static org.junit.
, 下一个选项是 "*"、"jupiter" 或 "platform"...
作为参考,以下是我的 IDE 中的示例测试:
@org.junit.jupiter.api.Test
void isButton() {
assertEquals()
}
知道如何解决这个问题吗?
谢谢!
Assertions
类的完整路径是:
org.junit.jupiter.api.Assertions.assertEquals
你的dependencies
块应该是这样的:
dependencies {
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3")
}
Intellij IDEA 和 JUnit 5 有一个很好的指南。看看它: Using JUnit 5 in IntelliJ IDEA
验证您在 POM 文件中指定的依赖项。 您应该在您的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>
元素。
这是一个简单的测试示例。
package work.basil.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}
junit-jupiter
神器请注意,从 JUnit 5.4.0 开始,我们可以指定junit-jupiter
的全新且非常方便的单个 Maven 工件,它将为您的项目提供 8 个库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.