此链接表示函数 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/