[英]custom error message for assertThat() in junit?
我想知道assertThat()是否有添加自定義錯誤消息的方法?
例如:
assertThat(file.exists(), is(equalTo(true)));
我想添加一些自定義消息,說明哪個文件名不存在
我寧願以下,以避免讀者相信你希望斷言文件名不存在..!
assertThat("File name should exist", file.exists(), is(equalTo(true)));
使用重載的assertThat
方法
assertThat("File name doesn't exist", file.exists(), is(equalTo(true)));
您可能只想使用帶有2個args的assertTrue()
方法:
Assert.assertTrue("File "+file.getAbsoluePath()+"does not exist", file.exists());
我更喜歡這個,因為人們可以把它讀作句子:“斷言文件'myFile'存在:myFile存在是真的”
assertThat("File '" + myFile + "' exists", myFile.exists(), is(true));
當失敗時,還會獲得包含所有必要信息的可讀消息:
java.lang.AssertionError: File '/blah' exists
Expected: is <true>
but: was <false>
我用這個方法:
Throwable thrown = catchThrowable(() -> myTestedObject.funtion());
assertThat("Error Message", thrown, isInstanceOf(MyException.class));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.