您可以实现TestRule#apply
并使用isTest
和isSuite
方法Description
来确定Statement
您TestRule
的应用类型。
这是一个示例界面,您可以构建一个具有完整before
, after
, verify
, beforeClass
, afterClass
,verifyClass
类型行为的规则:
public interface CombinedRule extends TestRule {
default Statement apply(Statement base, Description description) {
if (description.isTest()) {
return new Statement() {
public void evaluate() throws Throwable {
before();
try {
base.evaluate();
verify();
} finally {
after();
}
}
};
}
if (description.isSuite()) {
return new Statement() {
public void evaluate() throws Throwable {
beforeClass();
try {
base.evaluate();
verifyClass();
} finally {
afterClass();
}
}
};
}
return base;
}
default void before() throws Exception {
//let the implementer decide whether this method is useful to implement
}
default void after() {
//let the implementer decide whether this method is useful to implement
}
/**
* Only runs for Tests that pass
*/
default void verify() {
//let the implementer decide whether this method is useful to implement
}
default void beforeClass() throws Exception {
before();
}
default void afterClass() {
after();
}
/**
* Only runs for Suites that pass
*/
default void verifyClass() {
verify();
}
}