Junit执行器Runner探索之旅

这篇具有很好参考价值的文章主要介绍了Junit执行器Runner探索之旅。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

单元测试是每个程序员必备的技能,而Runner是每个单元测试类必有属性。本文通过解读Junit源码,介绍junit中每个执行器的使用方法,让读者在单元测试时,可以灵活的使用Runner执行器。

一、背景

在今年的敏捷团队建设中,京东物流通过Suite执行器实现了一键自动化单元测试。Juint除了Suite执行器还有哪些执行器呢?由此京东物流的Runner探索之旅开始了!

二、RunWith

RunWith的注释是当一个类用@RunWith注释或扩展一个用@RunWith注释的类时,JUnit将调用它引用的类来运行该类中的测试,而不是内置到JUnit中的运行器,就是测试类根据指定运行方式进行运行。

代码如下:

public @interface RunWith {
    Class<? extends Runner> value();
}


其中:Runner 就是指定的运行方式。

三、Runner

Runner的作用是告诉Junit如何运行一个测试类,它是一个抽象类。通过RunWith 指定具体的实现类,如果不指定默认使用BlockJUnit4ClassRunner,Runner的代码如下:

public abstract class Runner implements Describable {
    public abstract Description getDescription();
    public abstract void run(RunNotifier notifier);
    public int testCount() {
        return getDescription().testCount();
    }
}


3.1 ParentRunner

ParentRunner是一个抽象类,提供了大多数特定于运行器的功能,是经常使用运行器的父节点。实现了Filterable,Sortable接口,可以过滤和排序子对象。

提供了3个抽象方法:

protected abstract List<T> getChildren();
protected abstract Description describeChild(T child);
protected abstract void runChild(T child, RunNotifier notifier);


3.1.1 BlockJUnit4ClassRunner

BlockJUnit4ClassRunner是Juint4默认的运行器,具有与旧的测试类运行器(JUnit4ClassRunner)完全相同的行为。

ParentRunner3个抽象方法的实现如下:

@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
    Description description = describeChild(method);
    if (isIgnored(method)) {
        notifier.fireTestIgnored(description);
    } else {
        runLeaf(methodBlock(method), description, notifier);
    }
}
@Override
protected Description describeChild(FrameworkMethod method) {
    Description description = methodDescriptions.get(method);


    if (description == null) {
        description = Description.createTestDescription(getTestClass().getJavaClass(),
                testName(method), method.getAnnotations());
        methodDescriptions.putIfAbsent(method, description);
    }


    return description;
}
@Override
protected List<FrameworkMethod> getChildren() {
    return computeTestMethods();
}


runChild() :

  • 调用describeChild()

  • 判断方法是否包含@Ignore注解,有就触发TestIgnored事件通知

  • 构造Statement回调,通过methodBlock()构造并装饰测试方法

  • 执行测试方法调用statement.evaluate()

describeChild() : 对测试方法创建Description并进行缓存

getChildren():返回运行测试的方法。 默认实现返回该类和超类上所有用@Test标注的未重写的方法

3.1.2 BlockJUnit4ClassRunnerWithParameters

BlockJUnit4ClassRunnerWithParameters是一个支持参数的BlockJUnit4ClassRunner。参数可以通过构造函数注入或注入到带注释的字段中。参数包含名称、测试类和一组参数。

private final Object[] parameters;
private final String name;
public BlockJUnit4ClassRunnerWithParameters(TestWithParameters test)
        throws InitializationError {
    super(test.getTestClass().getJavaClass());
    parameters = test.getParameters().toArray(
            new Object[test.getParameters().size()]);
    name = test.getName();
}


参数代码如下:

public class TestWithParameters {
    private final String name;
    private final TestClass testClass;
    private final List<Object> parameters;
    public TestWithParameters(String name, TestClass testClass,
            List<Object> parameters) {
        notNull(name, "The name is missing.");
        notNull(testClass, "The test class is missing.");
        notNull(parameters, "The parameters are missing.");
        this.name = name;
        this.testClass = testClass;
        this.parameters = unmodifiableList(new ArrayList<Object>(parameters));
    }


BlockJUnit4ClassRunnerWithParameters一般结合Parameterized使用

3.1.3 Theories

Theories允许对无限数据点集的子集测试某种功能。提供一组参数的排列组合值作为待测方法的输入参数。同时注意到在使用Theories这个Runner的时候,待测方法可以拥有输入参数,可以使您的测试更加灵活。

测试代码如下:

@RunWith(Theories.class)
public class TheoriesTest {
    @DataPoints
    public static String[] tables = {"方桌子", "圆桌子"};
    @DataPoints
    public static int[] counts = {4,6,8};
    @Theory
    public void testMethod(String table, int count){
        System.out.println(String.format("一套桌椅有一个%s和%d个椅子", table, count));
    }
}


运行结果:

图2 Theories测试代码的执行结果

3.1.4 JUnit4

JUnit4是Junit4默认执行器的别名,想要显式地将一个类标记为JUnit4类,应该使用@RunWith(JUnit4.class),而不是,使用@RunWith(BlockJUnit4ClassRunner.class)

3.1.5 Suite

Suite允许您手动构建包含来自许多类的测试的套件.通过Suite.SuiteClasses定义要执行的测试类,一键执行所有的测试类。

测试代码如下:

@RunWith(Suite.class)
@Suite.SuiteClasses({Suite_test_a.class,Suite_test_b.class,Suite_test_c.class })
public class Suite_main {
}
public class Suite_test_a {
    @Test
    public void testRun(){
        System.out.println("Suite_test_a_running");
    }
}
public class Suite_test_b {
    @Test
    public void testRun(){
        System.out.println("Suite_test_b_running");
    }
}
public class Suite_test_c {
    @Test
    public void testRun(){
        System.out.println("Suite_test_c_running");
    }
}


执行结果:

图3 Suite测试代码的执行结果

如结果所示:执行MainSuit时依次执行了Suite_test_a,Suite_test_b,Suite_test_c 的方法,实现了一键执行。

3.1.6 Categories

Categories在给定的一组测试类中,只运行用带有@ inclecategory标注的类别或该类别的子类型标注的类和方法。通过ExcludeCategory过滤类型。

测试代码如下:

public interface BlackCategory {}
public interface WhiteCategory {}


public class Categories_test_a {
    @Test
    @Category(BlackCategory.class)
    public void testFirst(){
        System.out.println("Categories_test_a_testFirst_running");
    }
    @Test
    @Category(WhiteCategory.class)
    public void testSecond(){
        System.out.println("Categories_test_a_testSecond_running");
    }
}


public class Categories_test_b {
    @Test
    @Category(WhiteCategory.class)
    public void testFirst(){
        System.out.println("Categories_test_b_testFirst_running");
    }
    @Test
    @Category(BlackCategory.class)
    public void testSecond(){
        System.out.println("Categories_test_b_testSecond_running");
    }
}


执行带WhiteCategory的方法

@RunWith(Categories.class)
@Categories.IncludeCategory(WhiteCategory.class)
@Categories.ExcludeCategory(BlackCategory.class)
@Suite.SuiteClasses( { Categories_test_a.class, Categories_test_b.class })
public class Categories_main {
}


运行结果:

图4 Categories测试代码WhiteCategory分组执行结果

执行带BlackCategory的方法

@RunWith(Categories.class)
@Categories.IncludeCategory(BlackCategory.class)
@Categories.ExcludeCategory(WhiteCategory.class)
@Suite.SuiteClasses( { Categories_test_a.class, Categories_test_b.class })
public class Categories_main {
}


运行结果:

图5 Categories测试代码BlackCategory分组执行结果

如运行结果所示,通过IncludeCategory,ExcludeCategory可以灵活的运行具体的测试类和方法。

3.1.7 Enclosed

Enclosed使用Enclosed运行外部类,内部类中的测试将被运行。 您可以将测试放在内部类中,以便对它们进行分组或共享常量。

测试代码:

public class EnclosedTest {
   @Test
    public  void runOutMethou(){
        System.out.println("EnclosedTest_runOutMethou_running");
    }
    public static class EnclosedInnerTest {
        @Test
       public  void runInMethou(){
            System.out.println("EnclosedInnerTest_runInMethou_running");
        }
    }
}


运行结果:没有执行内部类的测试方法。

图6 Enclosed测试代码的执行结果

使用Enclosed执行器:

@RunWith(Enclosed.class)
public class EnclosedTest {
   @Test
    public  void runOutMethou(){
        System.out.println("EnclosedTest_runOutMethou_running");
    }
    public static class EnclosedInnerTest {
        @Test
       public  void runInMethou(){
            System.out.println("EnclosedInnerTest_runInMethou_running");
        }
    }
}


执行结果:执行了内部类的测试方法。

图7 Enclosed测试代码的执行结果

3.1.8 Parameterized

Parameterized实现参数化测试。 运行参数化的测试类时,会为测试方法和测试数据元素的交叉乘积创建实例。

Parameterized包含一个提供数据的方法,这个方法必须增加Parameters注解,并且这个方法必
须是静态static的,并且返回一个集合Collection,Collection中的值长度必须相等。

测试代码:

@RunWith(Parameterized.class)
public class ParameterizedTest {
    @Parameterized.Parameters
    public static Collection<Object[]> initData(){
       return Arrays.asList(new Object[][]{
               {"小白",1,"鸡腿"},{"小黑",2,"面包"},{"小红",1,"苹果"}
       });
    }
    private String name;
    private int  count;
    private String food;


    public ParameterizedTest(String name, int count, String food) {
        this.name = name;
        this.count = count;
        this.food = food;
    }
    @Test
    public void eated(){
       System.out.println(String.format("%s中午吃了%d个%s",name,count,food));
    }
}


运行结果:

图8 Parameterized测试代码的执行结果

3.2 JUnit38ClassRunner

JUnit38ClassRunner及其子类是Junit4的内部运行器,有一个内部类OldTestClassAdaptingListener

实现了TestListener接口。

3.3 ErrorReportingRunner

ErrorReportingRunner也是Junit4运行错误时抛出的异常,代码如下:

private final List<Throwable> causes;


public ErrorReportingRunner(Class<?> testClass, Throwable cause) {
    if (testClass == null) {
        throw new NullPointerException("Test class cannot be null");
    }
    this.testClass = testClass;
    causes = getCauses(cause);
}


    private List<Throwable> getCauses(Throwable cause) {
        if (cause instanceof InvocationTargetException) {
            return getCauses(cause.getCause());
        }
        if (cause instanceof InitializationError) {
            return ((InitializationError) cause).getCauses();
        }
        if (cause instanceof org.junit.internal.runners.InitializationError) {
            return ((org.junit.internal.runners.InitializationError) cause)
                    .getCauses();
        }
        return Arrays.asList(cause);
    }


当junit运行错误时,会抛出ErrorReportingRunner,例如:

public Runner getRunner() {
    try {
        Runner runner = request.getRunner();
        fFilter.apply(runner);
        return runner;
    } catch (NoTestsRemainException e) {
        return new ErrorReportingRunner(Filter.class, new Exception(String
                .format("No tests found matching %s from %s", fFilter
                        .describe(), request.toString())));
    }
}


3.4 IgnoredClassRunner

IgnoredClassRunner是当测试的方法包含Ignore注解时,会忽略该方法。

public class IgnoredClassRunner extends Runner {
    private final Class<?> clazz;
    public IgnoredClassRunner(Class<?> testClass) {
        clazz = testClass;
    }
    @Override
    public void run(RunNotifier notifier) {
        notifier.fireTestIgnored(getDescription());
    }
    @Override
    public Description getDescription() {
        return Description.createSuiteDescription(clazz);
    }
}


IgnoredClassRunner的使用

public class IgnoredBuilder extends RunnerBuilder {
    @Override
    public Runner runnerForClass(Class<?> testClass) {
        if (testClass.getAnnotation(Ignore.class) != null) {
            return new IgnoredClassRunner(testClass);
        }
        return null;
    }
}


当测试时想忽略某些方法时,可以通过继承IgnoredClassRunner增加特定注解实现。

四、小结

Runner探索之旅结束了,可是单元测试之路才刚刚开始。不同的Runner组合,让单元测试更加灵活,测试场景更加丰富,更好的实现了测试驱动开发,让系统更加牢固可靠。

作者:京东物流 陈昌浩

来源:京东云开发者社区文章来源地址https://www.toymoban.com/news/detail-480994.html

到了这里,关于Junit执行器Runner探索之旅的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 机械臂速成小指南(五):末端执行器

    👨‍🏫🥰🥳需要机械臂相关资源的同学可以在评论区中留言哦🤖😽🦄 指南目录📖: 🎉🎉机械臂速成小指南(零点五):机械臂相关资源🎉🎉 机械臂速成小指南(零):指南主要内容及分析方法 机械臂速成小指南(一):机械臂发展概况 机械臂速成小指南(二):

    2024年02月03日
    浏览(28)
  • 【PostgreSQL内核学习(二十三)—— 执行器(ExecEndPlan)】

    声明 :本文的部分内容参考了他人的文章。在编写过程中,我们尊重他人的知识产权和学术成果,力求遵循合理使用原则,并在适用的情况下注明引用来源。 本文主要参考了 postgresql-10.1 的开源代码和《OpenGauss数据库源码解析》和《PostgresSQL数据库内核分析》一书   在这三

    2024年01月17日
    浏览(38)
  • 【PostgreSQL内核学习(二十一)—— 执行器(InitPlan)】

    声明 :本文的部分内容参考了他人的文章。在编写过程中,我们尊重他人的知识产权和学术成果,力求遵循合理使用原则,并在适用的情况下注明引用来源。 本文主要参考了 postgresql-10.1 的开源代码和《OpenGauss数据库源码解析》和《PostgresSQL数据库内核分析》一书   在【

    2024年01月16日
    浏览(35)
  • Camunda 7.x 系列【53】Job 执行器

    有道无术,术尚可求,有术无道,止于术。 本系列Spring Boot 版本 2.7.9 本系列Camunda 版本 7.19.0 源码地址:https://gitee.com/pearl-organization/camunda-study-demo Job Executor 即任务执行器,是 Camunda 中的一个调度组件,负责执行异步后台作业。 Job 表示 Job Executor 执行的某一作业,例如,在定

    2024年02月09日
    浏览(27)
  • xxl-job执行器无法自动注册

    问题描述 在springboot项目里配置了xxl-job2.3.0,但是执行器无法自动注册 yaml配置如下: 执行器无法自动注册到xxl-job-admin 排查过程 经过debug发现,是spring没有加载xxlJobExecutor这个Bean debug流程(SpringApplication.run()–SpringApplication.refreshContext()–SpringApplication.refresh() --SpringApplication

    2024年02月16日
    浏览(24)
  • Spring Boot 中的任务执行器是什么,如何使用

    Spring Boot 是一个非常流行的 Java 开发框架,它的核心理念是通过简单的配置和约定来提高开发效率。在很多情况下,我们需要在后台执行一些任务,比如异步处理、定时任务等等。为了简化这些任务的开发和管理,Spring Boot 提供了一个任务执行器(Task Executor)。 任务执行器

    2024年02月15日
    浏览(27)
  • 【源码分析】XXL-JOB的执行器的注册流程

    目的:分析xxl-job执行器的注册过程 流程: 获取执行器中所有被注解( @xxlJjob )修饰的 handler 执行器注册过程 执行器中任务执行过程 版本: xxl-job 2.3.1 建议:下载 xxl-job 源码,按流程图 debug 调试, 看堆栈信息并按文章内容理解执行流程 。 完整流程图: 部分流程图: 首先启

    2023年04月22日
    浏览(31)
  • 【微软】【ICLR 2022】TAPEX:通过学习神经 SQL 执行器进行表预训练

    重磅推荐专栏: 《大模型AIGC》;《课程大纲》 本专栏致力于探索和讨论当今最前沿的技术趋势和应用领域,包括但不限于ChatGPT和Stable Diffusion等。我们将深入研究大型模型的开发和应用,以及与之相关的人工智能生成内容(AIGC)技术。通过深入的技术解析和实践经验分享,

    2024年02月05日
    浏览(32)
  • MYSQL04高级_逻辑架构剖析、查询缓存、解析器、优化器、执行器、存储引擎

    ①. 服务器处理客户端请求 ②. 连接层 系统(客户端)访问MySQL服务器前,做的第一件事就是建立TCP连接 经过三次握手建立连接成功后,MySQL服务器对TCP传输过来的账号密码做身份认证、权限获取 用户名或密码不对,会收到一个Access denied for user错误,客户端程序结束执行用户名密码认

    2024年02月12日
    浏览(28)
  • xxl-job中在分片的时候项目模块如何开启多个执行器?

    在xxl-job中,要在分片时开启多个执行器,您需要按照以下步骤进行操作: 1.在xxl-job的项目中,找到对应的模块(即需要开启多个执行器的模块)。 2.在模块的配置文件(通常是application.properties或application.yml)中,找到以下属性: properties xxl.job.executor.appname=   xxl.job.executor

    2024年02月09日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包