org.junit.rules.ExternalResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of virtdata-lib-curves4 Show documentation
Show all versions of virtdata-lib-curves4 Show documentation
Statistical sampling library for use in virtualdataset libraries, based on apache commons math 4
package org.junit.rules;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
* 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
*/
public abstract class ExternalResource implements TestRule {
public Statement apply(Statement base, Description description) {
return statement(base);
}
private Statement statement(final Statement base) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
before();
try {
base.evaluate();
} finally {
after();
}
}
};
}
/**
* Override to set up your specific external resource.
*
* @throws Throwable if setup fails (which will disable {@code after}
*/
protected void before() throws Throwable {
// do nothing
}
/**
* Override to tear down your specific external resource.
*/
protected void after() {
// do nothing
}
}