Package org.junit.rules
Class ExternalResource
java.lang.Object
org.junit.rules.ExternalResource
- All Implemented Interfaces:
TestRule
- Direct Known Subclasses:
TemporaryFolder
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);
}
}
- Since:
- 4.7
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafter()Override to tear down your specific external resource.apply(Statement base, Description description) Modifies the method-runningStatementto implement this test-running rule.protected voidbefore()Override to set up your specific external resource.private Statement
-
Constructor Details
-
ExternalResource
public ExternalResource()
-
-
Method Details
-
apply
Description copied from interface:TestRuleModifies the method-runningStatementto implement this test-running rule.- Specified by:
applyin interfaceTestRule- Parameters:
base- TheStatementto be modifieddescription- ADescriptionof the test implemented inbase- Returns:
- a new statement, which may be the same as
base, a wrapper aroundbase, or a completely new Statement.
-
statement
-
before
Override to set up your specific external resource.- Throws:
Throwable- if setup fails (which will disableafter
-
after
protected void after()Override to tear down your specific external resource.
-