I am using TestNG and having one issue.

Test Case : I am checking validation message that if user leave email id blank & Click on Login , System should fire message "Please Enter Valid Email".

I am verifying this message using Selenium & Java.

Code Logic :

  If(Dynamicmsg.equals("Please Enter Valid Email"))   
       System.out.println("Email validation working");

       System.out.println("Email validation not working");


It works fine, When other message found, condition goes to else and print that Email validation not working.

But TestNG Report says :

Passed -1

What I want is I want to tell testNG that fail this test case in report as well if conditions do not match.

  • 1
    Throw an exception or just put this in your else statement: org.testng.Assert.fail("Email validation didn't work");
    – cakes88
    Jul 19, 2016 at 20:51

3 Answers 3


You need to add assertion in TestNG to check. Your code should be as below remove whole code and just add below 1 line code.

Assert.assertEquals("Wrong error message shown", "Please Enter Valid Email", driver.findElement(By.xpath("Your path of element")).getText());

In this Assert.assertEquals is function which has 3 arguments, 1st the error message which you what to display on failure, 2nd your expected error message and 3rd is you actual error message which will be text displayed by your element.

Edit -

Just saw you already have taken text in Dynamicmsg variable, so you can rewrite above line as below

Assert.assertEquals("Wrong error message shown", "Please Enter Valid Email", Dynamicmsg);
  • Ok Test case gets failed , but getting exception also : java.lang.AssertionError: The Email field is required. expected [Please Enter Valid Email] but found [Wrong error message shown] Feb 26, 2016 at 8:25
  • 1
    Looks like you are using different version of TestNG, rewrite the assert statement as Assert.assertEquals("Please Enter Valid Email", Dynamicmsg); or Assert.assertEquals(Dynamicmsg, "Please Enter Valid Email", "Wrong error message shown"); Feb 26, 2016 at 8:45
  • @HelpingHands Does this resolve your query? Feb 27, 2016 at 6:40
  • I tried both but getting exception. Working fine but exception should not be there. Exception is : java.lang.AssertionError: Wrong Message Shown Here expected [The Email field is required] but found [The Email field is required.] Feb 29, 2016 at 13:06
  • I think so you are going wrong way, When you want TestNG to mark as fail there are only 2 ways 1. when exception is thrown 2. when you call Assert.fail(); you want testcase to fail and not to throw exception both won't work together , because testng throws exception when test case fails Feb 29, 2016 at 13:15

To achieve this, you can use Assert.assertEquals as already answered above.

Syntax is:

Assert.assertEquals(actual value, Expected value, "Your debug message that you want to display in case of failure")

For you it failed because I guess there is additional character "." at the end of actual error message. But in your expected error message "." is not there so the assertion might have failed.


If you want the test case to show a failure in the TestNG report, but not terminate the test case you may use soft Assert option

            SoftAssert sa = new SoftAssert();
            sa.assertEquals(actualMsg,expectedMsg,"Email validation message is incorrect");
            //more code here 

This will not throw exceptions in your test case until assertAll is called and will show your test case as failed.

If you want our test case to immediately fail after your email validation is unsuccessful use hard assert

Assert.assertEquals(actual, expected, message);

Use try catch mechanism to handle the Assertion error.

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.