JUnit - 未调用RunListener

时间:2013-06-03 16:16:38

标签: java junit jar

我正在尝试收集有关在系统中运行的测试的数据。测试来自外部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);

和ExecutionListener是:

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());
    }
}

但是没有调用任何函数......所以(rl.toString()返回空字符串)。

我该如何解决?

提前致谢!

1 个答案:

答案 0 :(得分:0)

有同样的问题,不知道为什么,但这似乎不适用于JUnitCore .runClasses(),但适用于JUnitCore.run()。

我意识到这是一个4岁的孩子,但是考虑到我今天遇到这个问题,值得发布一个答案。

相关问题