Warm tip: This article is reproduced from stackoverflow.com, please click
java testng appium assert assertion

Can you make assertTrue error messages more specific?

发布于 2020-03-27 15:43:37

I use testNG's assertTrue quite a lot to verify for example if transactions are correct

public void verifyAmount(WebElement element, String someText){
assertTrue(element.getText().contains(someText));
}

and when it fails it says

java.lang.AssertionError: did not expect to find [true] but found[false]

Is it possible to change assertion error to say what exactly went wrong, not just true/false statement? Is it possible to make that message more specific? Is there a way to make assertion error say :

java.lang.AssertionError: did not expect to find [10.000 $] but found[3000 $]
Questioner
Vedran Zenzerović
Viewed
27
Stultuske 2020-01-31 17:17

You could turn this:

public void verifyAmount(WebElement element, String someText){
assertTrue(element.getText().contains(someText));
}

into:

public void verifyAmount(WebElement element, String someText, String message){
assertTrue(element.getText().contains(someText), message);
}

and pass the error message you feel should be given there.

EDIT: I'm used to using other assert methods, which take the message as first param, but Long Nguyen is right in his answer: TestNG accepts the message as last parameter of the assertTrue.