Debugging JUnit Internals-Collection of common programming errors
To create and debug a custom runner, you don’t need to modify JUnit, the Eclipse JUnit plugin or mess about with the Eclipse installation. You only need to add make sure the new runner is on the classpath, i.e. on the build path.
Usually, to create a custom runner you extend ParentRunner, or more usually BlockJUnit4ClassRunner. Using the following as an example, extending ParentRunner#runChild
(the method which actually executes the test):
public class MyBlockJUnit4ClassRunner extends BlockJUnit4ClassRunner {
public MyBlockJUnit4ClassRunner(Class klass) throws InitializationError {
super(klass);
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
System.out.println("before");
super.runChild(method, notifier);
System.out.println("after");
}
}
Then the test:
@RunWith(MyBlockJUnit4ClassRunner.class)
public class MyRunnerTest {
@Test public void testIt() {
System.out.println("test it");
}
}
This produces:
before
test it
after
For the Parameterized classes, it’s a little bit more complicated, but not very much.