java - 在 JUnit4 中使用assertThat

标签 java maven junit junit4

此链接表示函数 assertThat() 在 JUnit4 和 JUnit5 中都有。

https://www.baeldung.com/junit-assertions

但是,根据创建者团队提供的JUnit4源代码,不包含assertThat(): http://s.bl-1.com/h/cs6JWS1r?url=https://github.com/junit-team/junit4/tree/master/src/main/java/junit/framework

没有太多使用 Maven 或 JUnit,它们如何协调? 我有使用 JUnit4 和 Assert.assertThat 的起始代码,但从源代码中可以看出,assertThat() 并不作为函数存在。一个错误证实了这一点。这段代码应该可以开箱即用。

最佳答案

此处包含 assertThat 的代码:https://github.com/junit-team/junit4/blob/master/src/main/java/org/junit/Assert.java (该包与您查看的包不同,因此可能只是导入错误。)

但是,您可能需要将 Hamcrest 库与 JUnit 一起使用,assertThat() 才能工作,因为您需要适当的 Matcher

在您发布的同一页面上有一个指向此处的链接: https://www.baeldung.com/java-junit-hamcrest-guide

这里还有更多详细信息:https://github.com/junit-team/junit4/wiki/matchers-and-assertthat

关于java - 在 JUnit4 中使用assertThat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53107981/

相关文章:

java - idea无法导入maven项目

junit - Spock 模拟在 collabolator 中返回 null 但在功能方法中不返回

java - 测试两个迭代器的相等性?

java - 使用 Java 在 Android 项目中获取 AWS 凭证的最简单方法?

java - 如何在 Java 中使用 3DES 加密/解密?

java - 集成测试 Spring Web 应用程序

java.sql.SQLException : no such column with the value I'm trying to insert

java - WebSphere 尝试从 Internet 加载与 Spring 相关的模式

java - Wildfly 8 AS 中的热部署位置

java - 在 Espresso Java/Android 中等待匹配器