Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Configure JUnit Listeners

DZone's Guide to

How to Configure JUnit Listeners

A tutorial from one of our DZone users on how to set up JUnit listeners using Maven and JUnit APIs.

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

JUnit Listeners are classes which receive callbacks about test execution. These callbacks are executed  when there is any event in test execution lifecycle like just before the test has started execution, just after the test has finished, when the test has failed or when the test have passed.  The callbacks are very useful in implementing functionality like logging about the tests, preparing customized reports an filtering test results. In this article we will show how the JUnit listeners are configured.

There are couple of ways this can be configured:

  1. In a declarative way by using the maven-surefire-plugin
  2. In a programmatic way using JUnit APIs

Now whether we use maven-surefire-plugin or JUnit API, we need to create a class which extends a class RunListener

Here is the listener class :

package com.codereq.test;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;

public class CommonListener extends RunListener {

    public void testStarted(Description description) {
        System.out.println("\n\n\nStarting .... " + description.getMethodName());
    }

    public void testFinished(Description description) {
        System.out.println("Finished .... "+ description.getMethodName());
    }

    public void testRunFinished(Result result) {
        System.out.println("\n\n");
    }
}


Configuring maven-surefire-plugin

In the standard maven pom.xml, the maven-surefire-plugin is configured by adding a listener property with the name of the class extending the RunListener class

<build>
    <plugins>
        <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
            <configuration>
 <properties>
     <property>
                 <name>listener</name>
 <value>com.codereq.test.CommonListener</value>
     </property>
 </properties>
    </configuration>
        </plugin>
    </plugins>
</build>


This is most commonly used pattern to add a listener and execute tests.

Configuring JUnit Runner

Another way to configure the JUnit listener is to register the listener class with JUnitCore. Here is the example:

import org.junit.runner.JUnitCore;

public class MyTestRunner {
    public static void main(String args[]) {
        JUnitCore runner = new JUnitCore();
runner.addListener(new MyTestListener());
runner.run(SampleTest.class);
    }
}

SampleTest.java

import org.junit.Assert;
import org.junit.Test;

public class SampleTest {

    @Test
    public void process() {
    Assert.assertTrue(true);
    }

    @Test
    public void compute() {
    Assert.assertTrue(true);
    }
}

On execution of the MyTestRunner, you will get report like this:

Tests count : 2
Starting Test : process
Finishing Test : process
Starting Test : compute
Finishing Test : compute

So, these are the two ways JUnit listener can be configured.

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
junit ,java ,unit testing

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}