前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >spring单元测试运行多次

spring单元测试运行多次

作者头像
johnhuster的分享
发布2022-03-28 19:49:36
发布2022-03-28 19:49:36
63300
代码可运行
举报
文章被收录于专栏:johnhusterjohnhuster
运行总次数:0
代码可运行

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

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

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

代码语言:javascript
代码运行次数:0
复制
    @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
代码运行次数:0
复制
    @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
代码运行次数:0
复制
    protected Statement withPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next) {
         return new SpringRepeat(next, frameworkMethod.getMethod());
     }

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

代码语言:javascript
代码运行次数:0
复制
    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();
         }
     }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/01/15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档