繁体   English   中英

junit中assertThat()的自定义错误消息?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM