@Runwith

概念

testRunner : 运行测试类的类

@Runwith : 告诉junit的框架应该是使用哪个testRunner

testRunner类关系

自定义一个简单的testRunner的方法是继承junit默认的runner BlockJUnit4ClassRunner。

public class BlockJUnit4ClassRunner extends ParentRunner<FrameworkMethod> {
 
  @Override
  protected List<FrameworkMethod> getChildren() {
    // scan test class for methonds annotated with @Test
  }
 
  @Override
  protected Description describeChild(FrameworkMethod method) {
    // create Description based on method name
  }
 
  @Override
  protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
    if (/* method not annotated with @Ignore */) {
      // run methods annotated with @Before
      // run test method
      // run methods annotated with @After
    }
  }
}

简单的自定义:在没给测试之前打印日志。

public class MyTestRunner extends BlockJUnit4ClassRunner {
    /**
     * Creates a BlockJUnit4ClassRunner to run {@code klass}
     *
     * @param klass
     * @throws InitializationError if the test class is malformed.
     */
    public MyTestRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
        System.out.println("this is my cusstom test runner : " + method.toString());

        super.runChild(method, notifier);
    }
}

引用

  1. Understanding JUnit's Runner architecture
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 单元测试 单测定义 单元测试(Unit Testing)又称为模块测试, 是针对程序模块(软件设计的最小单位)来进...
    运维开发笔记阅读 2,023评论 0 2
  • JUnit Intro Android基于JUnit Framework来书写测试代码。JUnit是基于Java语...
    chandarlee阅读 2,306评论 0 50
  • 1.初级篇 1.1 单元测试 我们在编写大型程序的时候,需要写成千上万个方法或函数,这些函数的功能可能很强大,但我...
    流川枫AI阅读 2,498评论 0 8
  • 1、港行版本,软件让淘宝店家刷国行,原因是国行没有google服务,更省电一些; 3、刚拿到一晚上待机百分之二十多...
    Cruby阅读 396评论 1 0