Skip to content

Instantly share code, notes, and snippets.

@uklance
Created April 2, 2014 07:51
Show Gist options
  • Save uklance/9929709 to your computer and use it in GitHub Desktop.
Save uklance/9929709 to your computer and use it in GitHub Desktop.
BlockJUnit4ClassRunner events
#withAfterClasses1
#withBeforeClasses1
#beforeClass
#createTest1
#createTest2
#fireTestStarted1
#fireTestStarted2
#before
#test1
#after
#fireTestFinished1
#fireTestFinished2
#createTest1
#createTest2
#fireTestStarted1
#fireTestStarted2
#before
#test2
#after
#fireTestFinished1
#fireTestFinished2
#fireTestIgnored1
#fireTestIgnored2
#withBeforeClasses2
#afterClass
#withAfterClasses2
package sandbox.junit;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runner.notification.StoppedByUserException;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
public class RunNotifierJUnit4ClassRunner extends BlockJUnit4ClassRunner {
public RunNotifierJUnit4ClassRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
public void run(RunNotifier notifier) {
RunNotifier wrapper = new LoggingRunNotifier(notifier);
super.run(wrapper);
}
@Override
protected Statement withBeforeClasses(Statement statement) {
final Statement superStatement = super.withBeforeClasses(statement);
return new Statement() {
public void evaluate() throws Throwable {
System.out.println("#withBeforeClasses1");
superStatement.evaluate();
System.out.println("#withBeforeClasses2");
}
};
}
@Override
protected Statement withAfterClasses(Statement statement) {
final Statement superStatement = super.withAfterClasses(statement);
return new Statement() {
public void evaluate() throws Throwable {
System.out.println("#withAfterClasses1");
superStatement.evaluate();
System.out.println("#withAfterClasses2");
}
};
}
@Override
protected Object createTest() throws Exception {
System.out.println("#createTest1");
Object test = super.createTest();
System.out.println("#createTest2");
return test;
}
public static class LoggingRunNotifier extends RunNotifier {
private final RunNotifier delegate;
public LoggingRunNotifier(RunNotifier delegate) {
super();
this.delegate = delegate;
}
public void addFirstListener(RunListener listener) {
delegate.addFirstListener(listener);
}
public void addListener(RunListener listener) {
delegate.addListener(listener);
}
public void fireTestAssumptionFailed(Failure failure) {
delegate.fireTestAssumptionFailed(failure);
}
public void fireTestFailure(Failure failure) {
delegate.fireTestFailure(failure);
}
public void fireTestFinished(Description description) {
System.out.println("#fireTestFinished1");
delegate.fireTestFinished(description);
System.out.println("#fireTestFinished2");
}
public void fireTestIgnored(Description description) {
System.out.println("#fireTestIgnored1");
delegate.fireTestIgnored(description);
System.out.println("#fireTestIgnored2");
}
public void fireTestRunFinished(Result result) {
System.out.println("#fireTestRunFinished1");
delegate.fireTestRunFinished(result);
System.out.println("#fireTestRunFinished2");
}
public void fireTestRunStarted(Description description) {
System.out.println("#fireTestRunStarted1");
delegate.fireTestRunStarted(description);
System.out.println("#fireTestRunStarted2");
}
public void fireTestStarted(Description description) throws StoppedByUserException {
System.out.println("#fireTestStarted1");
delegate.fireTestStarted(description);
System.out.println("#fireTestStarted2");
}
public void pleaseStop() {
delegate.pleaseStop();
}
public void removeListener(RunListener listener) {
delegate.removeListener(listener);
}
}
}
package sandbox.junit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(RunNotifierJUnit4ClassRunner.class)
public class RunNotifierJUnit4ClassRunnerTest {
@BeforeClass
public static void beforeClass() {
System.out.println(" #beforeClass");
}
@Before
public void before() {
System.out.println(" #before");
}
@After
public void after() {
System.out.println(" #after");
}
@AfterClass
public static void afterClass() {
System.out.println(" #afterClass");
}
@Test
public void test1() {
System.out.println(" #test1");
}
@Test
public void test2() {
System.out.println(" #test2");
}
@Test @Ignore
public void ignore1() {
System.out.println(" #ignore");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment