JUnit4 源码阅读(一)

JUnit4 源码阅读(一)

简介

JUnit 是一个Java开源单元测试框架。在1997年,由 Erich Gamma 和 Kent Beck 开发完成。其中 Erich Gamma是GoF(Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides)成员之一。
Java中的继承内部类注解, 反射泛型异常以及线程在JUnit源码处处可见,阅读源码有助于加强我们的基础功底。同时,JUnit中使用到了许多的设计模式,如组合装饰职责链观察者模板方法等等,有助于我们设计模式的学习和理解。阅读源码同时,我们可以学习大师们的编程习惯,使我们的代码更加安全,简洁,规范…两个字更美.

JUnit体系结构

JUnit结构

基本注解

注解基础知识, 请看:Java 反射和注解的一些总结
After在测试方法执行后,要执行的方法。方法必须是公共非静态
AfterClass在测试类执行后,需要执行的方法。方法必须是公共静态
Before在测试方法执行前,要执行的方法。方法必须是公共非静态
BeforeClass在测试类执行前,需要执行的方法。方法必须是公共静态
Ignore 测试方法,或者测试类被忽略

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface After {
   
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AfterClass {
   
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Before {
   
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface BeforeClass {
   
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Ignore {
   
    /**
     * The optional reason why the test is ignored.
     */
    String value() default "";
}

Rule作用于非静态的公共作用域或方法,用于定义方法的测试规则
RuleClass作用域静态的公共作用域或方法,用于定义测试类的测试规则

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Rule {
   
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface ClassRule {
   
}

Test 作用一个公共非静态的测试方法

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Test {
   

    /**
     * Default empty exception
     */
    static class None extends Throwable {
        private static final long serialVersionUID = 1L;
        private None() {
        }
    }

    Class<? extends Throwable> expected() default None.class;

    long timeout() default 0L;
}

internal

internal

执行类构建器builders

builders中是抽象RunnerBuilder的几种实现,作用是基于我们的测试类(要测试的类)来构建对应的执行器Runner,其中AllDefaultPossibilitiesBuilder整合几种RunnerBuilder,用于根据测试类的信息(注解信息)来指定唯一的一个Builder来创建Runner。

public class AllDefaultPossibilitiesBuilder extends RunnerBuilder {
   
    private final boolean canUseSuiteMethod;

    public AllDefaultPossibilitiesBuilder(boolean canUseSuiteMethod) {
        this.canUseSuiteMethod = canUseSuiteMethod;
    }

    @Override
    public Runner runnerForClass(Class<?> testClass) throws Throwable {
        List<RunnerBuilder> builders = Arrays.asList(
                ignoredBuilder(),
                annotatedBuilder(),
                suiteMethodBuilder(),
                junit3Builder(),
                junit4Builder());

        for (RunnerBuilder each : builders) {
            Runner runner = each.safeRunnerForClass(testClass);
            if (runner != null) {
                return runner;
            }
        }
        return null;
    }

    protected JUnit4Builder junit4Builder() {
        return new JUnit4Builder();
    }

    protected JUnit3Builder junit3Builder() {
        return new JUnit3Builder();
    }

    protected AnnotatedBuilder annotatedBuilder() {
        return new AnnotatedBuilder(this);
    }

    protected IgnoredBuilder ignoredBuilder() {
        return new IgnoredBuilder();
    }

    protected RunnerBuilder suiteMethodBuilder() {
        if (canUseSuiteMethod) {
            return new SuiteMethodBuilder();
        }
        return new NullBuilder();
    }
}

首先是IgnoredBuilder,如果测试有@Ignore注解,产生的Runner是IgnoredClassRunner, 该测试类的测试被忽略,不会执行,实际的执行run方法,仅仅是一个测试忽略的通知Notifier

public 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值