java - JUnit - 未调用 RunListener

标签 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() 返回空字符串)。

我该如何修复它?

提前致谢!

最佳答案

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

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

关于java - JUnit - 未调用 RunListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16901268/

相关文章:

java - gradle 运行执行;已编译的 JAR 因 ClassNotFoundException 而崩溃

java - 只查找从特殊字符 java 开始的第一个单词

java - 将 Java jar 中文本文件的路径传递给 C 库?

java - Java类中的成员字段是否按声明顺序初始化?

java - 由 : org. springframework.beans.factory.UnsatisfiedDependencyException 引起:创建名称为 'myAppDao' 的 bean 时出错:

java - 使用 Ant 运行 JUnit 测试

java - 未找到参数化 JUnit4 测试的测试

maven - 将属性添加到 Maven 中的 jar list 的最简单方法

java - 查询 JVM 堆中的特定对象

java - 从端口的方法返回一个类而不是 void