原理解析
  Spring的主要测试框架的核心是TestContext,TestContextManager,TestExcutionListener接口,我们每次启动测试的时候都会创建TestContextManager,它实际上是管理了一个TestContext来负责持有一个当前测试的上下文,可以实现测试实例的依赖注入。TestContextManager还负责在测试中更新TestContext的状态并代理到TestExecutionListener,它是用来监控实际的执行(如依赖注入,管理实务等等)
  下面我们来看下Spring的源代码来看下执行单元测试时候Spring是如何工作的

  看TestContext的调用栈不难看出,执行SpringJUnit4ClassRunner的createTestContextManager来创建 TestContextManager

  在创建TestContext的时候,我们看下构造方法,关键的两步,首先解析测试类的注解,其次创建ContextLoader