org.junit.rules.TestWatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit Show documentation
Show all versions of junit 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 java.util.ArrayList;
import java.util.List;
import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.Description;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
/**
* TestWatcher 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 TestWatcher watchman= new TestWatcher() {
* @Override
* protected void failed(Throwable e, Description description) {
* watchedLog+= description + "\n";
* }
*
* @Override
* protected void succeeded(Description description) {
* watchedLog+= description + " " + "success!\n";
* }
* };
*
* @Test
* public void fails() {
* fail();
* }
*
* @Test
* public void succeeds() {
* }
* }
*
*
* @since 4.9
*/
public abstract class TestWatcher implements TestRule {
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List errors = new ArrayList();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
private void succeededQuietly(Description description,
List errors) {
try {
succeeded(description);
} catch (Throwable t) {
errors.add(t);
}
}
private void failedQuietly(Throwable t, Description description,
List errors) {
try {
failed(t, description);
} catch (Throwable t1) {
errors.add(t1);
}
}
private void skippedQuietly(AssumptionViolatedException e, Description description,
List errors) {
try {
skipped(e, description);
} catch (Throwable t) {
errors.add(t);
}
}
private void startingQuietly(Description description,
List errors) {
try {
starting(description);
} catch (Throwable t) {
errors.add(t);
}
}
private void finishedQuietly(Description description,
List errors) {
try {
finished(description);
} catch (Throwable t) {
errors.add(t);
}
}
/**
* Invoked when a test succeeds
*/
protected void succeeded(Description description) {
}
/**
* Invoked when a test fails
*/
protected void failed(Throwable e, Description description) {
}
/**
* Invoked when a test is skipped due to a failed assumption.
*/
protected void skipped(AssumptionViolatedException e, Description description) {
}
/**
* Invoked when a test is about to start
*/
protected void starting(Description description) {
}
/**
* Invoked when a test method finishes (whether passing or failing)
*/
protected void finished(Description description) {
}
}