Guy L
Guy L

Reputation: 2954

JUnit - RunListener is not called

I'm trying to collect data about tests that runs in the system. The tests are coming from external jar:

        Class<?> classToLoad = Class.forName ("tests.tests", true, child);
        RunListener rl = new ExecutionListener();
        JUnitCore jUnit = new JUnitCore();
        jUnit.addListener(rl);
        Result result = jUnit.runClasses(classToLoad);

and ExecutionListener is:

public class ExecutionListener extends RunListener
{
    private StringBuilder _strBuilder;

    public ExecutionListener() 
    {
        _strBuilder = new StringBuilder(); 
    }

    @Override
    public String toString()
    {
        return _strBuilder.toString();
    }

    /**
     * Called before any tests have been run.
     * */
    @Override
    public void testRunStarted(Description description) throws java.lang.Exception
    {
        _strBuilder.append("Number of testcases to execute : " + description.testCount());
    }

    /**
     *  Called when all tests have finished
     * */
    @Override
    public void testRunFinished(Result result) throws java.lang.Exception
    {
        _strBuilder.append("Number of testcases executed : " + result.getRunCount());
    }

    /**
     *  Called when an atomic test is about to be started.

     * */
    @Override
    public void testStarted(Description description) throws java.lang.Exception
    {
        _strBuilder.append("Starting execution of test case : "+ description.getMethodName());
    }

    /**
     *  Called when an atomic test has finished, whether the test succeeds or fails.
     * */
    @Override
    public void testFinished(Description description) throws java.lang.Exception
    {
        _strBuilder.append("Finished execution of test case : "+ description.getMethodName());
    }

    /**
     *  Called when an atomic test fails.
     * */
    @Override
    public void testFailure(Failure failure) throws java.lang.Exception
    {
        _strBuilder.append("Execution of test case failed : "+ failure.getMessage());
    }

    /**
     *  Called when a test will not be run, generally because a test method is annotated with Ignore.
     * */
    @Override
    public void testIgnored(Description description) throws java.lang.Exception
    {
        _strBuilder.append("Execution of test case ignored : "+ description.getMethodName());
    }
}

But none of the of the function are being called... So (rl.toString() returns empty string).

How can I fixed it?

Thanks in advance!

Upvotes: 3

Views: 1077

Answers (1)

Shawn H
Shawn H

Reputation: 36

Had the same issue, not sure why but this doesn't seem to work for JUnitCore .runClasses() but does work for JUnitCore.run().

I realize this is a 4 year old but given I ran across this today figured worth posting an answer.

Upvotes: 2

Related Questions