首页 > 编程学习 > Junit执行源码分析,junit是怎么跑起来的(二)

接上一篇【Junit执行源码分析,junit是怎么跑起来的】 https://blog.csdn.net/Aqu415/article/details/127494898 这里我们接着分析

org.junit.runner.JUnitCore#run(org.junit.runner.Runner)

这个方法

public Result run(Runner runner) {
        Result result = new Result();
        RunListener listener = result.createListener();
        notifier.addFirstListener(listener);
        try {
            notifier.fireTestRunStarted(runner.getDescription());
            // @A
            runner.run(notifier);
            notifier.fireTestRunFinished(result);
        } finally {
            removeListener(listener);
        }
        return result;
    }

@A:执行runer的run方法,会执行我们的待测试目标方法

上一篇说到
runner是 org.junit.internal.builders.AllDefaultPossibilitiesBuilder#runnerForClass得到的返回对象:

    public Runner runnerForClass(Class<?> testClass) throws Throwable {
        List<RunnerBuilder> builders = Arrays.asList(
                ignoredBuilder(),
                annotatedBuilder(),
                suiteMethodBuilder(),
                junit3Builder(),
                junit4Builder());

        for (RunnerBuilder each : builders) {
            Runner runner = each.safeRunnerForClass(testClass);
            if (runner != null) {
                return runner;
            }
        }
        return null;
    }

junit默认提供了几种Runner,比如听过的Junit3和熟悉的Junit4(其他的没听过 哈哈);
这里会决定返回哪一种 Runner;
从设计模式上来说这里运用的策略模式:每一种runner会根据目标测试类判解析是否需要自己来处理,如果是由自己来处理则返回一个Runner类;就好像说:嗯,你是我的菜,并把菜装在篮子里返回

举个例,我们看看 JUnit3Builder 是怎么判的该类是自己的菜的

public class JUnit3Builder extends RunnerBuilder {
    @Override
    public Runner runnerForClass(Class<?> testClass) throws Throwable {
        if (isPre4Test(testClass)) {
            return new JUnit38ClassRunner(testClass);
        }
        return null;
    }

    boolean isPre4Test(Class<?> testClass) {
        return junit.framework.TestCase.class.isAssignableFrom(testClass);
    }
}

如果类上标注了 @TestCase注解,那么这个类就会由JUnit3执行

**我们看看JUnit4Builder **

public class JUnit4Builder extends RunnerBuilder {
    @Override
    public Runner runnerForClass(Class<?> testClass) throws Throwable {
        return new BlockJUnit4ClassRunner(testClass);
    }
}

看代码JUnit4是一个兜底的方案,其他Runner不吃的菜全部由他来吃;嗯 了不起

BlockJUnit4ClassRunner

上面我们看到了JUnit4Builder 返回了一个BlockJUnit4ClassRunner对象;

在这里插入图片描述
BlockJUnit4ClassRunner继承了 ParentRunner,ParentRunner的run方法就是执行测试类方法的入口(上面标记@A的地方)。

org.junit.runners.ParentRunner#run
    @Override
    public void run(final RunNotifier notifier) {
        EachTestNotifier testNotifier = new EachTestNotifier(notifier,
                getDescription());
        try {
            Statement statement = classBlock(notifier);
            statement.evaluate();
        } catch (AssumptionViolatedException e) {
            testNotifier.addFailedAssumption(e);
        } catch (StoppedByUserException e) {
            throw e;
        } catch (Throwable e) {
            testNotifier.addFailure(e);
        }
    }
org.junit.runners.ParentRunner#classBlock
    protected Statement classBlock(final RunNotifier notifier) {
        // @B
        Statement statement = childrenInvoker(notifier);
        // @C
        if (!areAllChildrenIgnored()) {
            statement = withBeforeClasses(statement);
            statement = withAfterClasses(statement);
            statement = withClassRules(statement);
        }
        return statement;
    }

@B:找到标记了 @Test注解的方法
@C:把测试类里标注了 @BeforeClass(静态方法) 、@AfterClass(静态方法)等注解的方法找到并包装成 Statement对象,设计模式应该属于责任链模式,形成一个链式数据结构

org.junit.runners.BlockJUnit4ClassRunner#runChild

跟了一圈代码最终方法执行到
org.junit.runners.BlockJUnit4ClassRunner#runChild

    @Override
    protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
        Description description = describeChild(method);
        // @A
        if (isIgnored(method)) {
            notifier.fireTestIgnored(description);
        } else {
         // @B
            runLeaf(methodBlock(method), description, notifier);
        }
    }

@A:被忽略的方法,不被执行
@B:org.junit.runners.BlockJUnit4ClassRunner#methodBlock

    protected Statement methodBlock(FrameworkMethod method) {
        Object test;
        try {
            test = new ReflectiveCallable() {
                @Override
                protected Object runReflectiveCall() throws Throwable {
                    return createTest();
                }
            }.run();
        } catch (Throwable e) {
            return new Fail(e);
        }

        Statement statement = methodInvoker(method, test);
        statement = possiblyExpectingExceptions(method, test, statement);
        statement = withPotentialTimeout(method, test, statement);
        statement = withBefores(method, test, statement);
        statement = withAfters(method, test, statement);
        statement = withRules(method, test, statement);
        return statement;
    }

这里跟上面ParentRunner行为差不多,会把目标类里标注了 @Before、@After的方法和目标方法串联起来,这里区别的是:ParentRunner是把标注了@BeforeClass、和@AfterClass的静态方法串联起来。

todo~~

Copyright © 2010-2022 dgrt.cn 版权所有 |关于我们| 联系方式