JUnit RunListener Example
In JUnit RunListener Example, we shall show users how they can add RunListener
to the test cases. There are cases when we want to respond to the events during a test case run. Here we can extend the RunListener
class and override the methods according to our implementation. The JUnit RunListener
can listen to the events of the JUnit lifecycle.
If somehow, listener throws an error, then it will be removed from remainder of the test case run. Let’s see the example.
Furthermore, if user wants to learn about the basic of JUnit, they are advised to visit the below examples from Java Code Geeks.
- JUnit Hello World Example
- JUnit FixMethodOrder Example
- JUnit Keyboard Input Example
- JUnit MultiThreaded Test Example
- JUnit Group Tests Example
1. Technology Stack
We have used following technologies for this example to work.
- Java
- JUnit 4.12
- Maven – build and dependency tool
- Eclipse – IDE for writing code
2. Project Setup
You may skip project creation and jump directly to the beginning of the example below.
Create a new maven project
Select File -> New -> Maven Project
Click on Next button. Fill in the details as detailed below:
With the click on Finish button, we are ready to start coding for this example.
3. JUnit RunListener Example
First of all we need to provide the JUnit jar to the project. For this we add following lines to the pom.xml
pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
Now, we will create a class which will extends the RunListener
class. This class has many methods that we can override. It is our wish which methods to implement and which to ignore.
For the sake of knowledge of users, we have taken all methods here and write about them. You can skip some of them. Code of this class is self explanatory.
OurListener.java
package junitrunlistener; import org.junit.runner.Description; import org.junit.runner.Result; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunListener; public class OurListener extends RunListener { // Called before any tests have been run. public void testRunStarted(Description description) throws java.lang.Exception { System.out.println("Test cases to execute : " + description.testCount()); } // Called when all tests have finished public void testRunFinished(Result result) throws java.lang.Exception { System.out.println("Test cases executed : " + result.getRunCount()); } // Called when an atomic test is about to be started. public void testStarted(Description description) throws java.lang.Exception { System.out.println("Execution Started : " + description.getMethodName()); } // Called when an atomic test has finished, whether the test succeeds or // fails. public void testFinished(Description description) throws java.lang.Exception { System.out.println("Execution Finished : " + description.getMethodName()); } // Called when an atomic test fails. public void testFailure(Failure failure) throws java.lang.Exception { System.out.println("Execution Failure : " + failure.getException()); } // Called when a test will not be run, generally because a test method is // annotated with Ignore. public void testIgnored(Description description) throws java.lang.Exception { System.out.println("Execution Ignored : " + description.getMethodName()); } // Called when an atomic test flags that it assumes a condition that is false public void testAssumptionFailure(Failure failure){ System.out.println("Assumption Failure : " + failure.getMessage()); } }
3.1. Test Classes
We will create 2 test classes for this example.
TestClassA.java
It has 2 test methods, test_A_1()
and test_A_2()
.
package junitrunlistener; import static org.junit.Assert.assertTrue; import org.junit.Test; public class TestClassA { @Test public void test_A_1(){ assertTrue(1==2); } @Test public void test_A_2(){ assertTrue(true); } }
Due to line no 11(highlighted), test_A_1()
method fails and throws java.lang.AssertionError
.
TestClassB.java
This class also have 2 methods, test_B_1()
and test_B_2()
.
package junitrunlistener; import static org.junit.Assert.assertTrue; import org.junit.Ignore; import org.junit.Test; public class TestClassB { @Test public void test_B_1(){ assertTrue(true); } @Ignore @Test public void test_B_2(){ assertTrue(2==5); } }
As you can see that test_B_2()
is marked with @Ignore
annotation. This annotation will simply ignore this test case from running.
3.2. Main Class
TestClassRun.java
Now, we are ready to run our tests. Create a class with the following code.
package junitrunlistener; import org.junit.runner.JUnitCore; public class TestClassRun { public static void main(String[] args) { JUnitCore runner = new JUnitCore(); runner.addListener(new OurListener()); runner.run(TestClassA.class, TestClassB.class); } }
Here, we have used JUnitCore
class of JUnit to run the test cases. This is required as we need to add our custom listener to the test cases. See the highlighted line in above class.
3.2.1. Output
Test cases to execute : 4 Execution Started : test_A_1 Execution Failure : java.lang.AssertionError Execution Finished : test_A_1 Execution Started : test_A_2 Execution Finished : test_A_2 Execution Started : test_B_1 Execution Finished : test_B_1 Execution Ignored : test_B_2 Test cases executed : 3
It is cleared from the output that with each test case, testStarted()
and testFinished()
methods are called.
One of the test is failed due to the condition which we have passed in TestClassA
class.
4. Conclusion
We have learnt in this example, that by using a custom listener in JUnit we can log and do tasks accordingly on the basis of methods executed. Like, if you want to call or notify the user that a particular test case is failed, you can simply write that piece of code in testFailure()
method.
It is also very useful in logging purpose.
5. Download the Eclipse Project
This is JUnit RunListener Example.
You can download the full source code of this example here: JUnitRunListenerExample.zip
if any testcases fail then skipp all testcase from same class.