Scott
Scott

Reputation: 9488

Debugging JUnit Internals

I'm wanting to dive into the internals of JUnit so that I can work towards creating a custom runner. I've attached the source to the JUnit within eclipse, but it seems that it's a slightly different build. How do I go about "adding" a plugin for JUnit within eclipse which will sync up with the version of attached source that I have? Is it as simple as creating a folder in the plugins directory, dropping in the jar file and restarting eclipse, then modifying the target runtime?

Upvotes: 2

Views: 340

Answers (2)

Matthew Farwell
Matthew Farwell

Reputation: 61705

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.

Upvotes: 3

alexsmail
alexsmail

Reputation: 5813

Please consult this link http://alexsmail.blogspot.com/2012/01/how-to-install-eclipse-english.html

For the Eclipse 3.7 (Indigo) download JUnit jars from here ftp://ftp.osuosl.org/pub/eclipse/eclipse/updates/3.7/R-3.7.1-201109091335/plugins/ (junit, source files and jdt).

Upvotes: 1

Related Questions