JUnit4源码学习笔记

先上一个在Spring-test下运行的调用栈
自底向上:
JUnitStarter
IDEA对JUnit的支持,调用JUnitCore.run(Runner),将注解@RunWith指定的Runner作为参数传给JUnit。该方法注释“Do not use. Testing purposes only.”,
 
JUnitCore
运行JUnit测试的入口。通过调用run()方法启动测试。
 
Runner
testcase运行的容器。JUnit默认使用BlockJUnit4ClassRunner。
鼓励扩展实现自己的Runner。例如Spring-test扩展BlockJUnit4ClassRunner实现了SpringJUnit4ClassRunner。
 
RunNotifier
在testcase开始、结束、成功、失败、忽略时,通知JUnit。
 
RunListener
测试运行事件的监听器。默认只有一个listener,在Result类内部定义,用来记录测试开始时间、耗时、成功个数、失败个数。
如果需要自定义的listener,扩展RunListener,并通过JUnitCore.addListener()添加。
 
Runner.run()
看下run方法的实现
  
  public void run(final RunNotifier notifier) {
        EachTestNotifier testNotifier = new EachTestNotifier(notifier,
                getDescription());
        try {
            Statement statement = classBlock(notifier);
            statement.evaluate();
        } catch (AssumptionViolatedException e) {
            testNotifier.fireTestIgnored();
        } catch (StoppedByUserException e) {
            throw e;
        } catch (Throwable e) {
            testNotifier.addFailure(e);
        }
    }
EachTestNotifier
没看懂这个类的用意,内部类也没有文档,可能是为了以后扩展留的吧。
 
Statement
一个Statement是运行测试时的一段指令。看下classBlock()的实现:
protected Statement classBlock(final RunNotifier notifier) {
    Statement statement = childrenInvoker(notifier);
    statement = withBeforeClasses(statement);
    statement = withAfterClasses(statement);
    statement = withClassRules(statement);
    return statement;
}
classBlock构建了一个Statement的责任链。
第一个statement: 将所有@Test标记的方法在RunnerScheduler中执行,默认的实现是串行执行的。有没有并行执行的实现?
第二个statement:将@BeforeClass标记的方法封装成Statement,并放在责任链第一位。
第三个statement:将@AfterClass标记的方法封装成Statement,并放在责任链第一位。但是RunAfters的实现会先执行fNext(即上一步封装的RunBefores),再执行自己封装的方法。这里的实现跟当初设计fNext这个域的初衷有点不一致。
第四个statement:@ClassRule注解,工作中没用过,改天再详细了解。
 
接下来就是依次执行statement链了。
 
 
原文地址:https://www.cnblogs.com/night1989/p/9531739.html