I would prefer the following, to avoid the reader believing that you wish to assert that the file name doesn't exist..!
assertThat("File name should exist", file.exists(), is(equalTo(true)));
Question
I was wondering if assertThat() has a way of adding a custom error message ?
for example in this :
assertThat(file.exists(), is(equalTo(true)));
I would like to add some custom message, saying which file name doesnt exist
Solution
I would prefer the following, to avoid the reader believing that you wish to assert that the file name doesn't exist..!
assertThat("File name should exist", file.exists(), is(equalTo(true)));
OTHER TIPS
Use the overloaded assertThat
method
assertThat("File name doesn't exist", file.exists(), is(equalTo(true)));
I prefer this, since one can read it as a sentence: "assert that file 'myFile' exist: myFile exists is true"
assertThat("File '" + myFile + "' exists", myFile.exists(), is(true));
And one gets also readable message with all necessary information when it fails:
java.lang.AssertionError: File '/blah' exists
Expected: is <true>
but: was <false>
You may want to simply use the assertTrue()
methods with 2 args:
Assert.assertTrue("File "+file.getAbsoluePath()+"does not exist", file.exists());
I use this method :
Throwable thrown = catchThrowable(() -> myTestedObject.funtion());
assertThat("Error Message", thrown, isInstanceOf(MyException.class));