JUnit
如果你正在使用 JUnit 执行,则可以扩展 TestWatcher
类:
public class TestRules extends TestWatcher {
@Override
protected void failed(Throwable e, Description description) {
// This will be called whenever a test fails.
}
所以在你的测试类中你可以简单地调用它:
public class testClass{
@Rule
public TestRules testRules = new TestRules();
@Test
public void doTestSomething() throws Exception{
// If the test fails for any reason, it will be caught be testrules.
}