Junit RuleChain、TestRule机制学习

TestRule

   你可以对junit进行扩展,也就是对于测试的方法进行拦截处理,默认junit实现有几个实现,你可以自己扩展

使用方式如下:

@Rule
public MyTestRule rule = new MyTestRule("MyTestRule");

 

public class MyTestRule implements TestRule {
	private String name;

	public MyTestRule(String name) {
		this.name = name;
	}

	@Override
	public Statement apply(final Statement base, Description description) {
		System.out.println(name);
		return new Statement() {
			@Override
			public void evaluate() throws Throwable {
				System.out.println("before");
				base.evaluate();
				System.out.println("after");
			}
		};
	}

}

 RuleChain 

   假如你测试代码中需要多个rule,可以使用RuleChain,它还可以控制多个rule的执行顺序

@Rule
	public RuleChain chain = RuleChain.outerRule(new MyTestRule("111111111111"))
			.around(new MyTestRule("22222222222222222")).around(new MyTestRule("33333333333333333333"));

 执行属性:3、2、1,先执行外层的,参考RuleChain源码你看就明白 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值