@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
-
How does the error collector rule work?
The ErrorCollector rule allows the execution of a test to continue after the first problem is found.
-
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.
-
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.
-
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!