Table of contents
1.
Introduction
2.
Understanding Error Collector
3.
@Rule Annotation
4.
Examples
4.1.
Test without Error Collector
4.2.
Output 
4.3.
Test1 using Error Collector
4.4.
Output 
4.5.
Test2 using Error Collector
4.6.
Output
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

JUnit Error Collector

Author Aditya Anand
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! In this blog, we will learn about error collectors in JUnit. It is a crucial concept since it allows testers to cover the entire line of code in a test regardless of the occurrence of failure at any point, which is very important because, as a tester, we want to check the output of each and every line in the test.
While writing a test script, if any line of code fails due to network failure, assertion failure, or any other reason. In that situation, you can still continue executing the test script using a special feature provided by JUnit known as “error collector.”
So even if a test fails in the middle, it can still continue executing it and will provide all errors at once.

Let's get started, and I hope you learn a lot from this tutorial.

Understanding Error Collector

As its name suggests, it collects errors in all the assert statements. When an assert failure is found, it proceeds with the next statements and only returns the failure at the end of the test.

JUnit assertions have a drawback, since when an assert fails, the whole test fails, leaving some tests conditions untested. The ErrorCollector comes to address this problem.

With the JUnit error collector, you can continue the test execution even after an issue is found or the test fails. Error collector collects all error objects and reports them only once after the test execution is over.

@Rule Annotation

JUnit provides @Rule annotation, which is used to create an object of error collector. Once the object for the error collector is created, you can easily add all the errors into the object using the method addError (Throwable error). 

The unit provides a special kind of handling of tests, Test Case or test suites by using @rule annotation. Using @rule, you can easily add or redefine the behavior of the test.

JUnit API provides several built-in rules that a tester can use, or even you can write your own rule.

Examples

Now let’s see example test code to understand the concepts better.

Test without Error Collector

package com.aditya04848.junit.helper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

public class TestErrorCollector {

    @Test
    public void testWithAssert() {
        assertEquals(1, 2);
        assertTrue(false);
        assertFalse(false);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output 

We can clearly see in the output that only one failed assertEquals is executed because JUnit will stop executing a test if any error is found in that test.

Test1 using Error Collector

Now let's try to solve above faced problem using an error collector. We are using addError() method to add a new throwable into the error collector. Throwable is basically a superset of the Exception class.

package com.aditya04848.junit.helper;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

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

    @Test
    public void testWithErrorCollector() {
        collector.addError(new Throwable("example error 1"));  
        collector.addError(new Throwable("example error 2"));
        collector.addError(new Throwable("example error 3"));
        collector.addError(new Throwable("example error 4"));
       
    }
}
You can also try this code with Online Java Compiler
Run Code

Output 

We can see here in the output that all four sample errors are executed even if they belong to the same test.

Test2 using Error Collector

An example test 2, we will write our test using checkThat() method, and see the functioning of the error collector.

package com.aditya04848.junit.helper;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

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

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

    @Test
    public void testWithErrorCollector() {
        collector.checkThat(1, equalTo(2));
        collector.checkThat(true, is(false));
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

We can see that both checkThat are executed even if the first checkThat fails.

FAQs

  1. How does the error collector rule work?
    The ErrorCollector rule allows the execution of a test to continue after the first problem is found.
     
  2. How do I use JUnit assertion?
    JUnit assertions can be used to asset functions of GUI, for example, assertEquals can compare strings, objects, etc. similarly assertTrue assert that a condition is true.
     
  3. What is the use of a method intercept rule in JUnit?
    Junit Rules work on the principle of AOP (aspect-oriented programming). It intercepts the test method, thus providing an opportunity to do some stuff before or after executing a particular test method.
     
  4. What is the return value of the matcher in Java?
    The matches() method of Matcher Class return a boolean value.

Key Takeaways

In conclusion, we can create a new error collector under @Rule annotation. When we encounter a failure inside a test, we can simply add this to the error collector, and in this way, we can test all lines of code inside a test. 

I encourage you to check JUnit Introduction with Eclipse and JUnit Time Test blogs for further reading.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

Live masterclass