DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Why Testing is a Long-Term Investment for Software Engineers
  • TestNG vs. JUnit: A Comparative Analysis of Java Testing Frameworks
  • Mastering Unit Testing and Test-Driven Development in Java
  • JUnit, 4, 5, Jupiter, Vintage

Trending

  • Building a Real-Time Audio Transcription System With OpenAI’s Realtime API
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • How to Merge HTML Documents in Java
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  1. DZone
  2. Coding
  3. Java
  4. How to Configure JUnit Listeners

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.

By 
Gaurav Srivastava user avatar
Gaurav Srivastava
·
Feb. 26, 16 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
21.0K Views

Join the DZone community and get the full member experience.

Join For Free

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.

JUnit

Opinions expressed by DZone contributors are their own.

Related

  • Why Testing is a Long-Term Investment for Software Engineers
  • TestNG vs. JUnit: A Comparative Analysis of Java Testing Frameworks
  • Mastering Unit Testing and Test-Driven Development in Java
  • JUnit, 4, 5, Jupiter, Vintage

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!