Custom error message for assertThat () in junit?

I was wondering if assertThat () can add a custom error message?

eg:

assertThat(file.exists(), is(equalTo(true))); 

I would like to add some kind of custom message stating which file name does not exist

+9
java junit
source share
5 answers

I would prefer the following: avoid reading, believing that you want to claim that the file name does not exist ..!

 assertThat("File name should exist", file.exists(), is(equalTo(true))); 
+14
source share

Use the overloaded assertThat method

 assertThat("File name doesn't exist", file.exists(), is(equalTo(true))); 
+10
source share

You can simply use the assertTrue() methods with two arguments:

 Assert.assertTrue("File "+file.getAbsoluePath()+"does not exist", file.exists()); 
+2
source share

I prefer this, as it can be read as a sentence: "claim that the file" myFile "exists: myFile exists true"

 assertThat("File '" + myFile + "' exists", myFile.exists(), is(true)); 

And a read message also appears with all the necessary information when it fails:

 java.lang.AssertionError: File '/blah' exists Expected: is <true> but: was <false> 
+2
source share

I am using this method:

 Throwable thrown = catchThrowable(() -> myTestedObject.funtion()); assertThat("Error Message", thrown, isInstanceOf(MyException.class)); 
0
source share

All Articles