`
lizhensan
  • 浏览: 369631 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Junit RuleChain、TestRule机制学习

    博客分类:
  • java
 
阅读更多

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源码你看就明白 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics