tags:

views:

267

answers:

2

I have some test infrastructure classes that I'd like to add as listeners of JUnitCore , specifically for testRunFinished. I'm invoking Junit 4 from ant's task.

Is there some way for me to get access to the JUnitCore created by the task so that I can add a listener?

+1  A: 

Not from ant's junit task.

You best write a main method that runs your test suite "manually".

package test;

import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;

public class RollYourOwnTestRun {

    public static void main(String[] args) {
        Runner runner = Request.classes(StackTest.class).getRunner();
        RunNotifier notifier = new RunNotifier();
        Result result= new Result();
        RunListener listener= result.createListener();
        notifier.addListener(listener);
        notifier.addListener(...); // add your listener
        notifier.fireTestRunStarted(runner.getDescription());
        runner.run(fNotifier);
        notifier.fireTestRunFinished(result);
    }

}
Adrian
A: 

A @RunWith annotation could help (with some minor API best-practice violations): you give your own Runner, and override run(RunNotifier notifier). Through the RunNotifier, you may use the add*Listener-API, which is marked as internal only currently. Good luck!

ShiDoiSi