spring单元测试运行多次

2022-03-28 19:49:36 浏览数 (1)

spring-test框架丰富了junit测试,本文要谈的内容就是spring-test框架里面的Repeat注解,该注解完成的操作就是指定某个单元测试方法执行多次,具体用法如下:

代码语言:javascript复制
@Repeat(指定的运行次数)
  @Test
  public void test(){
.........
}

接下来看下@Repeat这个注解的工作原理,之所以能够工作离不开一个重要的类SpringJUnit4ClassRunner,该类是spring-test框架对junit测试框架的扩展,该类重写了BlockJUnit4ClassRunner类的runChild方法,该方法是junit测试框架运行具体单元测试时调用的一个方法,具体SpringJUnit4ClassRunner做了哪些重要变动,下面还是给出核心代码:

代码语言:javascript复制
    @Override
     protected void runChild(FrameworkMethod frameworkMethod, RunNotifier notifier) {
         Description description = describeChild(frameworkMethod);
         if (isTestMethodIgnored(frameworkMethod)) {
             notifier.fireTestIgnored(description);
         }
         else {
             Statement statement;
             try {
                 statement = methodBlock(frameworkMethod);
             }
             catch (Throwable ex) {
                 statement = new Fail(ex);
             }
 runLeaf(statement, description, notifier);
         }
     }

SpringJUnit4ClassRunner的runChild方法首先对每一个要执行的方法做了些增强,具体来看下methodBlock这个方法:

代码语言:javascript复制
    @Override
     protected Statement methodBlock(FrameworkMethod frameworkMethod) {
         Object testInstance;
         try {
             testInstance = new ReflectiveCallable() {
                 @Override
                 protected Object runReflectiveCall() throws Throwable {
                     return createTest();
                 }
             }.run();
         }
         catch (Throwable ex) {
             return new Fail(ex);
         }
        Statement statement = methodInvoker(frameworkMethod, testInstance);
         statement = possiblyExpectingExceptions(frameworkMethod, testInstance, statement);
         statement = withBefores(frameworkMethod, testInstance, statement);
         statement = withAfters(frameworkMethod, testInstance, statement);
         statement = withRulesReflectively(frameworkMethod, testInstance, statement);
         statement = withPotentialRepeat(frameworkMethod, testInstance, statement);
         statement = withPotentialTimeout(frameworkMethod, testInstance, statement);
         return statement;
     }

上面标红部分分别对应了@Before @After @Repeat等注解,这里就不再讨论其他注解,下面就看下@Repeat注解的原理:

代码语言:javascript复制
    protected Statement withPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next) {
         return new SpringRepeat(next, frameworkMethod.getMethod());
     }

SpringRepeat完成了对Statement的增强,采用的是包装器这个设计模式,具体相关代码在SpringRepeat类的evaluate方法里:

代码语言:javascript复制
    public void evaluate() throws Throwable {
         for (int i = 0; i < this.repeat; i  ) {
             if (this.repeat > 1 && logger.isInfoEnabled()) {
                 logger.info(String.format("Repetition %d of test %s#%s()", (i   1),
                         this.testMethod.getDeclaringClass().getSimpleName(), this.testMethod.getName()));
             }
             this.next.evaluate();
         }
     }

0 人点赞