org.junit.rules.TestWatchman Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit-dep Show documentation
Show all versions of junit-dep Show documentation
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.
package org.junit.rules;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
/**
* TestWatchman is a base class for Rules that take note of the testing
* action, without modifying it. For example, this class will keep a log of each
* passing and failing test:
*
*
* public static class WatchmanTest {
* private static String watchedLog;
*
* @Rule
* public MethodRule watchman= new TestWatchman() {
* @Override
* public void failed(Throwable e, FrameworkMethod method) {
* watchedLog+= method.getName() + " " + e.getClass().getSimpleName()
* + "\n";
* }
*
* @Override
* public void succeeded(FrameworkMethod method) {
* watchedLog+= method.getName() + " " + "success!\n";
* }
* };
*
* @Test
* public void fails() {
* fail();
* }
*
* @Test
* public void succeeds() {
* }
* }
*
*/
public class TestWatchman implements MethodRule {
public Statement apply(final Statement base, final FrameworkMethod method,
Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
starting(method);
try {
base.evaluate();
succeeded(method);
} catch (Throwable t) {
failed(t, method);
throw t;
} finally {
finished(method);
}
}
};
}
/**
* Invoked when a test method succeeds
*
* @param method
*/
public void succeeded(FrameworkMethod method) {
}
/**
* Invoked when a test method fails
*
* @param e
* @param method
*/
public void failed(Throwable e, FrameworkMethod method) {
}
/**
* Invoked when a test method is about to start
*
* @param method
*/
public void starting(FrameworkMethod method) {
}
/**
* Invoked when a test method finishes (whether passing or failing)
*
* @param method
*/
public void finished(FrameworkMethod method) {
}
}