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-

What is error collector in JUnit?
What is @Rule in jUnit?
Example using ErrorCollector
Benefits of JUnit ErrorCollector

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 :

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:

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 TestRunner.java Let’s add above test class in a test runner and execute it to collect all errors. See below code: Output: See the failure trace which traces all the errors in one place:

Benefits of JUnit ErrorCollector

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

assertEquals(String message, Object expected, Object actual) which compare that two objects are equals. 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.