2

I am new to jUnit and Selenium. Currently, we are doing a POC in our project for evaluating the tools' suitability.

One of the key requirements for us would be use friendly messages in case of assertion failures and verify Failures. The objective is to have those analyzed by manual testers by keeping those user intuitive.

I am trying out SLF4J for logging INFO messages for user friendly messages.

The challenge I am facing is that - when assertions fail, how to pass a custom message instead of jUnit default message?

For example, I want to get rid of the following default assertion failure message

expected "Health A-Z" to match glob "Health A-ZZ" (had transformed the glob into regexp "Health A-ZZ"

and frame it as

The title of the Menu Item doesn't match the expected value "Health A-ZZ". The actual value seen is "Health A-Z"

Question1 - Is there a way I override the default message with what I want? Question2 - Can I pass variable parameters to the message and make it a dynamic message?

Sample Code:

try {
    assertEquals("Health A-ZZ", selenium.getText("//div[@id='topnav']/ul/li[2]/a"));
    logger.info("SUCCESS: Menu Item {} title verification Passed. EXPECTED : A-Z and Actual: {}",mainCounter, selenium.getText("//div[@id='topnav']/ul/li[2]/a"));
}

catch (AssertionError e) {
    logger.error("FAILURE: Menu Item {} title verification Failed. EXPECTED : A-ZZ and Actual: {}",mainCounter, selenium.getText("//div[@id='topnav']/ul/li[2]/a"));
}

I get the assertFailure message printed out twice.. One with the error message in the catch block above and the other, the default junit assertfailre message like the one I mentioned above I wanted to get rid of.

Thanks in Advance.

Regards, Prakash

1 Answer 1

5

You can use the Junit assert function. assertEquals(message, expected, actual)

This will print the message that has been given in the message String in case of failure. You can also pass String variable to the "message".

5
  • Thank you so much Varun. I happened to see this function in the documentation. But, I wasn't quite clear how to pass the message with variable parameters.
    – Prakash
    Aug 2, 2011 at 14:25
  • assertEquals("Expected value: " + expected + "Doesn't match the actual: " + actual, expected, actual) I am going to give it a try.. is my above representation correct?. Can I log the messaeg using slf4j logging API as well?
    – Prakash
    Aug 2, 2011 at 14:29
  • I tried this approach but the message given within assertEquals function call isn't displayed when the assert failed.
    – Prakash
    Aug 2, 2011 at 19:22
  • Rather, the assertfailed error exception is caught in the ctach block and the messages printed there were triggered. Is this the right behavior?
    – Prakash
    Aug 2, 2011 at 19:23
  • Sorry for late reply. I am new to stackoverflow. So dont know how t check responses and all. Anyways normally I havent tried putting the assert function into a try-catch block. You can try checking the result after removing the try-catch block . Aug 8, 2011 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.