All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.rules.ExternalResource Maven / Gradle / Ivy

Go to download

JUnit is a regression testing framework written by Erich Gamma and Kent Beck. It is used by the developer who implements unit tests in Java.

There is a newer version: 4.11
Show newest version
package org.junit.rules;

import org.junit.runners.model.FrameworkMethod;
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);
 * 	}
 * }
 * 
*/ public abstract class ExternalResource implements MethodRule { public final Statement apply(final Statement base, FrameworkMethod method, Object target) { return new Statement() { @Override public void evaluate() throws Throwable { before(); try { base.evaluate(); } finally { after(); } } }; } /** * Override to set up your specific external resource. * @throws if setup fails (which will disable {@code after} */ protected void before() throws Throwable { // do nothing } /** * Override to tear down your specific external resource. * @throws if setup fails (which will disable {@code after} */ protected void after() { // do nothing } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy