org.junit.rules
Class ExternalResource

java.lang.Object
  extended by org.junit.rules.TestRule
      extended by org.junit.rules.ExternalResource
Direct Known Subclasses:
TemporaryFolder

public abstract class ExternalResource
extends TestRule

A base class for Rules (like TemporaryFolder) that set up an external resource before a test (a file, socket, server, database connection, etc.), and guarantee to tear it down afterward:

 public static class UsesExternalResource {
        Server myServer= new Server();
 
        @Rule
        public ExternalResource resource= new ExternalResource() {
                @Override
                protected void before() throws Throwable {
                        myServer.connect();
                };
 
                @Override
                protected void after() {
                        myServer.disconnect();
                };
        };
 
        @Test
        public void testFoo() {
                new Client().run(myServer);
        }
 }
 


Constructor Summary
ExternalResource()
           
 
Method Summary
protected  void after()
          Override to tear down your specific external resource.
protected  Statement apply(Statement base, Description description)
          Modifies the method-running Statement to implement this test-running rule.
protected  void before()
          Override to set up your specific external resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExternalResource

public ExternalResource()
Method Detail

apply

protected Statement apply(Statement base,
                          Description description)
Description copied from class: TestRule
Modifies the method-running Statement to implement this test-running rule.

Specified by:
apply in class TestRule
Parameters:
base - The Statement to be modified
description - A Description of the test implemented in base
Returns:
a new statement, which may be the same as base, a wrapper around base, or a completely new Statement.

before

protected void before()
               throws Throwable
Override to set up your specific external resource.

Throws:
if - setup fails (which will disable after
Throwable

after

protected void after()
Override to tear down your specific external resource.