Search code examples
javatestngappiumassertassertion

Can you make assertTrue error messages more specific?


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 $]

Solution

  • 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.