JUnit Stopwatch Rule Example

0

Stopwatch is a JUnit TestRule that tracks the time as each test is run. Just when the test is about to run, the current time in nanoseconds is stored so that when the test finishes, the time taken for execution can be determined.

  1. Store the current time
  2. Run the test
  3. Test succeeds – Stopwatch.succeeded() is called
  4. Test fails – Stopwatch.failed() is called
  5. Test is skipped – Stopwatch.skipped() is called
  6. Test is finished – Stopwatch.finished() is always called once the test is done with its execution

In the below test class, we create an instance of Stopwatch annotated with @Rule and use it to determine the execution time. Each test that the class contains will result in either success or failure. Test test4Skipped() throws AssumptionViolatedException so that JUnit can skip the test.
In longRunningTest(), we call Stopwatch.runtime() to calculate the run time of each statement. This will be useful if we have many running statements and we want to know how much time each statement took.

StopwatchExample:

package com.javarticles.junit;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.AssumptionViolatedException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Stopwatch;
import org.junit.runner.Description;

public class StopwatchExample {
    @Rule
    public final Stopwatch stopwatch = new Stopwatch() {
        protected void succeeded(long nanos, Description description) {
            System.out.println(description.getMethodName() + " succeeded, time taken " + nanos);
        }

        /**
         * Invoked when a test fails
         */
        protected void failed(long nanos, Throwable e, Description description) {
            System.out.println(description.getMethodName() + " failed, time taken " + nanos);
        }

        /**
         * Invoked when a test is skipped due to a failed assumption.
         */
        protected void skipped(long nanos, AssumptionViolatedException e,
                Description description) {
            System.out.println(description.getMethodName() + " skipped, time taken " + nanos);
        }

        /**
         * Invoked when a test method finishes (whether passing or failing)
         */
        protected void finished(long nanos, Description description) {
            System.out.println(description.getMethodName() + " finished, time taken " + nanos);
        }

    };

    @Test
    public void test1Ok() {
        System.out.println("Start test1");
        doAction();
        System.out.println("end test1");
    }

    @Test
    public void test2Ok() {
        System.out.println("Start test1");
        doAction();
        System.out.println("end test2");
    }

    @Test
    public void test3Fail() {
        System.out.println("Start test3");
        doAction();
        Assert.fail("Some error");
        System.out.println("end test3");
    }
    
    @Test
    public void test4Skipped() {
        System.out.println("Start test3");
        doAction();
        throw new AssumptionViolatedException("skipped");
    }
    
    @Test
    public void longRunningTest() {
        System.out.println("Start long running test");
        doAction(1000);
        System.out.println("After action1 " + stopwatch.runtime(TimeUnit.SECONDS) + " secs");
        doAction(3000);
        System.out.println("After action2 " + stopwatch.runtime(TimeUnit.SECONDS) + " secs");
    }
    
    private void doAction(long mls) {
        try {
            Thread.sleep(mls);
        } catch (InterruptedException e) {
        }
    }
    
    private void doAction() {
        doAction(200);
    }
}

Output:

Start long running test
After action1 1 secs
After action2 4 secs
longRunningTest succeeded, time taken 4005218963
longRunningTest finished, time taken 4005218963
Start test1
end test1
test1Ok succeeded, time taken 200188461
test1Ok finished, time taken 200188461
Start test1
end test2
test2Ok succeeded, time taken 200042877
test2Ok finished, time taken 200042877
Start test3
test3Fail failed, time taken 200063783
test3Fail finished, time taken 200063783
Start test3
test4Skipped skipped, time taken 200800824
test4Skipped finished, time taken 200800824

Download the source code

This was an example about JUnit Stopwatch Rule.

You can download the source code here: junitStopwatchExample.zip

About Author

Ram's expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring. You can reach him at rsatish.m@gmail.com

Comments are closed.