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 - Android,如何模糊/玻璃/霜当前 Activity

java - testNG 没有可运行的方法

java - 抛出预期异常后测试响应

java - 当响应不包含媒体类型 header 时,Spring WebClient 抛出 "Only one connection receive subscriber allowed"

java - Jena Fuseki API 将新数据添加到现有数据集 [java]

java - 自动填充集

java - 如何从 jar 文件运行 Maven Failsafe 集成测试?

java - 当我尝试运行 JUnit 测试时,为什么会收到 "An exception was thrown while searching for persistence archives with ClassLoader"错误?

eclipse - Eclipse 中带有 Profile 的多模块 Maven 无法解决对子项的依赖

java - 在 Spring Web 应用程序中运行 JUnit 测试时指定 Maven 配置文件