JUnit ExternalResource Rule Example


ExternalResource is an abstract test rule that allows us to set up external resources before the test depends on, for example,a temporary file, socket, server or a database connection.
The set up should be run before the test starts and clean up after the test ends. In order to do the set up and clean up, you need to override before() and after() methods.

Next, you need to define a public final member, create an instance of the external resource and annotate the member with @Rule annotation.

See TestName Example to know more about test rules.

ExternalResource Example

In this example, we create a Jetty server resource by extending ExternalResource. The server resource knows how to can be started and stopped. Any test that depends on Jetty server could simply use this resource as a a final instance variable, annotated with @Rule.

We must annotate the field with @Rule as this is the only way JUnit can find out all the rules that gets applied on the test.


package com.javarticles.junit;

import org.eclipse.jetty.server.Server;
import org.junit.rules.ExternalResource;

public class JettyServerResource extends ExternalResource {
    private Server jettyServer = new Server(9000);
    protected void before() throws Throwable {
        System.out.println("Start jettyServer");
        System.out.println("JettyServer started");

    protected void after() {
        if (this.jettyServer.isRunning()) {
            try {
                System.out.println("Stop jettyServer");
                System.out.println("JettyServer stopped");
            } catch (Exception e) {
                System.out.println("Exception while stopping JettyServer");

In our test class, we define a final variable and annotate it with @Rule. The rule will automatically run before and after the test and the test doesn’t need to do anything else.


package com.javarticles.junit;

import org.junit.Rule;
import org.junit.Test;

public class ExternalResourceExample {
    public final JettyServerResource server = new JettyServerResource();
    public void someTestThatNeedsJettyServer() {
        System.out.println("Started someTestThatNeedsJettyServer");

You can see from the output, jetty server starts first and then the test is run. Once the test is done with its execution, server is stopped.


2016-02-01 14:22:33.184:INFO::main: Logging initialized @182ms
Start jettyServer
2016-02-01 14:22:33.210:INFO:oejs.Server:main: jetty-9.3.7.RC1
2016-02-01 14:22:33.262:INFO:oejs.ServerConnector:main: Started [email protected]{HTTP/1.1,[http/1.1]}{}
2016-02-01 14:22:33.263:INFO:oejs.Server:main: Started @261ms
JettyServer started
Started someTestThatNeedsJettyServer
Stop jettyServer
2016-02-01 14:22:33.267:INFO:oejs.ServerConnector:main: Stopped [email protected]{HTTP/1.1,[http/1.1]}{}
JettyServer stopped

Download the source code

This was an example about ExternalResource rule.

You can download the source code here: junitExternalResourceRuleExample.zip

Comments are closed.