Sunday 2 November 2014

Reporting Error in Junit Selenium

Usage of Assertion in Junit Selenium :
Reporting error in junit can be done using assertion, if we are using if-else statement  even if the result comes as fail but in junit report will show as pass with no error (Green). So, here we need to use assertion to report error.

Assert.assertEquals(expected , actual)
Assert- It is an in-built class in JAVA
assertEquals- It is a static function inside Assert class in Junit

Drawback of assertion :
If we execute this line (Assert.assertEquals(expected , actual)) directly, then line after Assert.assertEquals(expected , actual) will not be executed if assertion fails.

Steps to overcome the drawbacks of assertion :
So, to overcome this assertion we need to surround this assertion with try catch block. So, that the below line after assertion statement should get printed even if assertion fails.

Here, again issue comes like the report not showing the error or failure.By, writing catch statment we have recovered from error. Then we need to write a JAVA code to report the error with the help of errorCollector class.

errorCollector- It is an in-built class in Junit, above it give annotation @Rule.
errorCollector.addError(t)- It means, whenever an error is made, then the object of throwable is made.Then, now error will be reported in report of Junit.

Another one assert function-
Assert.assertTrue("message",condition)- It will print the error message only when the condition fails.

Here is one sample example showing error reporting in Junit :
import junit.framework.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
public class UnderstandingAssertions {

    @Rule
    public ErrorCollector errCollector = new ErrorCollector();

    @SuppressWarnings("deprecation")
    @Test
    public void testFriendlistFacebook(){
        int actual_total_friends=100; //selenium
        int expected_total_friends=1001; //xlsx file
      
        /*if(actual_total_friends==expected_total_friends){
            System.out.println("Pass");
        }else{
            System.out.println("Fail"); //using if else statement, in report it will show as pass with no error for that we need assertion
            //report the error
        }*/
      
        System.out.println("A");
        try{
            Assert.assertEquals(expected_total_friends, actual_total_friends);
        }catch(Throwable t){
            System.out.println("ERROR ENCOUNTERED");
            errCollector.addError(t);
        }
        System.out.println("B");
      
        try{
            Assert.assertEquals("A", "B");
        }catch(Throwable t){
            System.out.println("ERROR ENCOUNTERED");
            errCollector.addError(t);
        }
      
        try{
            Assert.assertEquals("A", "A");
        }catch(Throwable t){
            System.out.println("ERROR ENCOUNTERED");
            errCollector.addError(t);
        }
      
        try{
            Assert.assertTrue("error msg", 4>9);
        }catch(Throwable t){
            System.out.println("ERROR");
            errCollector.addError(t);
        }
    }
}

No comments:

Post a Comment