public abstract class

ExternalResource

extends Object
implements MethodRule
java.lang.Object
   ↳ org.junit.rules.ExternalResource
Known Direct Subclasses

Class Overview

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);
 	}
 }
 

Summary

Public Constructors
ExternalResource()
Public Methods
final Statement apply(Statement base, FrameworkMethod method, Object target)
Modifies the method-running Statement to implement an additional test-running rule.
Protected Methods
void after()
Override to tear down your specific external resource.
void before()
Override to set up your specific external resource.
[Expand]
Inherited Methods
From class java.lang.Object
From interface org.junit.rules.MethodRule

Public Constructors

public ExternalResource ()

Public Methods

public final Statement apply (Statement base, FrameworkMethod method, Object target)

Modifies the method-running Statement to implement an additional test-running rule.

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.

Protected Methods

protected void after ()

Override to tear down your specific external resource.

protected void before ()

Override to set up your specific external resource.

Throws
setup fails (which will disable after
Throwable