org.junit.rules
Class ExternalResource

java.lang.Object
  extended by org.junit.rules.ExternalResource
All Implemented Interfaces:
MethodRule
Direct Known Subclasses:
TemporaryFolder

public abstract class ExternalResource
extends Object
implements MethodRule

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
 Statement apply(Statement base, FrameworkMethod method, Object target)
          Modifies the method-running Statement to implement an additional test-running rule.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExternalResource

public ExternalResource()
Method Detail

apply

public final Statement apply(Statement base,
                             FrameworkMethod method,
                             Object target)
Description copied from interface: MethodRule
Modifies the method-running Statement to implement an additional test-running rule.

Specified by:
apply in interface MethodRule
Parameters:
base - The Statement to be modified
method - The method to be run
target - The object on with the method will be run.
Returns:
a new statement, which may be the same as base, a wrapper around base, or a completely new Statement.