• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

In a normal scenario, whenever you identify any error during test execution, you would stop the test, fix the error and re-run the test.

But JUnit has a slightly different approach. With JUnit error collector, you can still continue with the test execution even after an issue is found or test fails. Error collector collects all error objects and reports it only once after the test execution is over.

In this tutorial, you will learn-

Why use Error Collector?

While writing a test script, you want to execute all the tests even if any line of code fails due to network failure, assertion failure, or any other reason. In that situation, you can still continue executing test script using a special feature provided by JUnit known as "error collector."

For this, JUnit uses @Rule annotation which is used to create an object of error collector. Once the object for error collector is created, you can easily add all the errors into the object using method addError (Throwable error). As you know, that Throwable is the super class of Exception and Error class in Java. When you add errors in this way, these errors will be logged in JUnit test result .

The benefit of adding all errors in an Error Collector is that you can verify all the errors at once. Also, if the script fails in the middle, it can still continue executing it

Note: In the case of using simple assert or try/catch block , using error collector method won't be possible.

Sample code

To understand more on Error Collector, see below code example which demonstrates how to create an Error Collector object and add all the errors in that object to track the issue :

package guru99.junit;		

import org.junit.Rule;		
import org.junit.Test;		
import org.junit.rules.ErrorCollector;		

public class ErrorCollectorExample {				
    @Rule		
    public ErrorCollector collector = new ErrorCollector();							

    @Test		
    public void example() {					
    collector.addError(new Throwable("There is an error in first line"));							
    collector.addError(new Throwable("There is an error in second line"));							
    collector.checkThat(getResults(),			
                not(containsString("here is an error")));			
    // all lines of code will execute and at the end a combined failure will		
    be logged in.		
    }		
}	

What is @Rule in jUnit?

JUnit provides special kind of handling of tests, Test Case or test suite by using @rule annotation. Using @rule, you can easily add or redefine the behaviour of the test.

There are several built-in rules provided by JUnit API that a tester can use, or even you can write our own rule.

See below line of code, which shows how to use @rule annotation along with Error Collector:

@Rule				
public ErrorCollector collector= new ErrorCollector();

Example using ErrorCollector

To understand error collector, let's create a class and a rule to collect all the errors. You will add all the errors using addError(throwable) here.

See below code which simply creates a rule which is nothing but creating "Error Collector object." Which is further used to add all the errors in order to report the issue at the end:

ErrorCollectorExample.java

package guru99.junit;		

import org.junit.Assert;		
import org.junit.Rule;		
import org.junit.Test;		
import org.junit.rules.ErrorCollector;		

public class ErrorCollectorExample {				
    @Rule		
    public ErrorCollector collector = new ErrorCollector();							

    @Test		
    public void example() {					
    collector.addError(new Throwable("There is an error in first line"));							
    collector.addError(new Throwable("There is an error in second line"));							

        System.out.println("Hello");					
        try {			
            Assert.assertTrue("A " == "B");					
        } catch (Throwable t) {					
            collector.addError(t);					
        }		
        System.out.println("World!!!!");					
    }		
}		

TestRunner.java

Let's add above test class in a test runner and execute it to collect all errors. See below code:

package guru99.junit;		

import org.junit.runner.JUnitCore;		
import org.junit.runner.Result;		
import org.junit.runner.notification.Failure;		

public class TestRunner {				
			public static void main(String[] args) {									
      Result result = JUnitCore.runClasses(ErrorCollectorExample.class);					
			for (Failure failure : result.getFailures()) {							
         System.out.println(failure.toString());					
      }		
      System.out.println("Result=="+result.wasSuccessful());							
   }		
}      

Output:

See the failure trace which traces all the errors in one place:

JUnit ErrorCollector

Benefits of JUnit ErrorCollector

You can use JUnit assertion for functional or GUI validation e.g.

  1. assertEquals(String message, Object expected, Object actual) which compare that two objects are equals.
  2. Similarly, assertTrue(Boolean condition) asserts that a condition is true.

Using assertion, validation test becomes easy. But one major issue is that test execution will stop even if a single assertion fails.

Test continuity and recovery handling is crucial to test automation success. Error Collector is the best way to handle such kind of scenarios.

Summary:

  • Junit error collector allows a test to continue even after the first issue is found and test fails at the end
  • Error collector collects all error objects and reports it only, after all, the test execution over
  • The benefit of adding all errors in an Error Collector is that you can verify all the errors at once
  • Error collector simply adds errors using method addError(throwable err) provided by ErrorCollector.java.

 

YOU MIGHT LIKE: