ErrorCollector in JUnit

ErrorCollector in JUnit is a rule that allows execution of a test to continue after the first problem is found.


As we have seen in Assertion Tutorial, Program stop execution if an assertion fails in JUnit. So suppose you have included Assertion in the very starting off your Test case and it fails, So all line code after this assertion will not execute. So execution halts when the assertion fails.

To overcome from this problem we use the Concept of Error Collector class, as it continues the execution even though the assertions fails it also report the error in JUnit report card.


Error collector objective is to collect all the error which comes up with script execution and report it only at the end.

The object of this class is created with the @Rule annotation. You can add errors by calling the method addError(Throwable error), which adds a Throwable to the table. Throwable is the parent class of all errors and exceptions in java. When you add errors like this, these errors will be automatically logged to JUnit test results as well.


Let’s understand it with an easy example. Here we have two test methods firstTest() andsecondTest() . While we are failing firstTest() explicitly and using error collector in this class to report the error. If you will remove the error collector from catch block the error will occur but nor reported because catch will handle it.


Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply