org.junit.rules
Class ExternalResource
java.lang.Object
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);
}
}
ExternalResource
public ExternalResource()
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 modifiedmethod
- The method to be runtarget
- 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.