要使用Hamcrest和JUnit 4,我們使用assertThat()
函數。但是,JUnit 5不再具有assertThat()
函數。如何在沒有assertThat()
的情況下使用Hamcrest?當JUnit 5沒有assertThat()函數時,如何在JUnit 5中使用Hamcrest?
9
A
回答
12
您必須確保Hamcrest包含在classpath中,然後使用Hamcrest提供的assertThat()函數。從當前JUnit 5 User Guide - Writing Tests Assertions,
的JUnit木星的org.junit.jupiter.Assertions類不提供像在JUnit 4的org.junit.Assert 類中找到,它接受一個Hamcrest匹配器的 assertThat()方法。相反,鼓勵開發者 使用由 第三方斷言庫提供的對匹配器的內置支持。
以下示例演示如何在JUnit Jupiter測試中使用來自Hamcrest的assertThat()支持 。只要已將Hamcrest庫 添加到類路徑中,就可以靜態導入 (如assertThat(),is()和equalTo())方法,然後在測試 (如下面的assertWithHamcrestMatcher()方法)中使用它們。
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.jupiter.api.Test;
class HamcrestAssertionDemo {
@Test
void assertWithHamcrestMatcher() {
assertThat(2 + 1, is(equalTo(3)));
}
}
自然的基礎上,JUnit 4中的編程模型可以 繼續使用org.junit.Assert#assertThat傳統的測試。」
相關問題
- 1. 如何正確使用JUnit assertThat?
- 2. java.lang.NoSuchMethodError當使用JUnit測試運行5
- 3. JUnit Hamcrest斷言
- 4. 如何一起使用JUnit和Hamcrest?
- 5. junit和hamcrest聲明
- 6. 使用gradle從intellij升級JUnit 4到JUnit 5
- 7. Junit 5類似於codeception dataProvider?
- 8. 配置gradle使用JUnit,Mockito,Hamcrest和Espresso
- 9. 秩序都JUnit和Hamcrest
- 10. Iphone 5 Appium和jUnit - 應用程序沒有元素
- 11. 不能讓JUnit的工作(assertThat/hasItem)
- 12. 如何在Android上使用Hamcrest匹配器與jUnit
- 13. JUnit或Hamcrest是否有寬容斷言?
- 14. 如何使用Ant執行JUnit 5 @Tag測試?
- 15. 爲什麼在Maven項目中使用JUnit 5時必須使用maven-surefire-plugin?
- 16. 如何在junit 5 gradle測試報告中捕獲stdout/stderr?
- 17. 與completionTimeout沒有使用JUnit
- 18. 在JUnit 5中,如何在所有測試之前運行代碼
- 19. 如何在junit上測試失敗截圖5
- 20. 如何使用SD()函數在laravel 5
- 21. 當使用文件函數時,jUnit獲取NullPointerException
- 22. 具有多個源組的Gradle中的JUnit 5
- 23. eclipse中的Junit在使用junit時產生NoClassDefFoundError
- 24. Junit測試函數
- 25. Android JUnit在Eclipse JUnit視圖中沒有輸出
- 26. 如何將外部類的值傳遞給@Nested @Test int JUnit 5?
- 27. JUnit 5,Java 9和Gradle:如何傳遞--add-modules?
- 28. 當JUnit失敗時,如何停止Maven?
- 29. 如何在Junit中調用數據源
- 30. 如何在Junit中使用註釋 - eclipse
什麼吹拂我的心靈是,'assertThat'是應該是做所有斷言的新方法,然後突然出現JUnit 5,並以某種方式仍然擁有所有傳統的斷言方法,但也刪除了唯一的現代方法,他們已經致力於破壞API, d認爲他們真的可以到那裏去除所有冗餘的斷言方法我認爲這可能是爲了方便遷移,但不是,參數順序並不相同。 – Trejkaz