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