Java 未捕获异常处理程序-UiAutomator

Java 未捕获异常处理程序-UiAutomator,java,junit,exception-handling,android-uiautomator,uncaught-exception,Java,Junit,Exception Handling,Android Uiautomator,Uncaught Exception,我想让UiAutomator在测试失败时截图。我目前的设置方式如下: @Override protected void setUp() throws Exception { System.out.println("Beginning setup..."); testFailed = true; } @Override protected void tearDown() throws Exception { if(testFailed) getUi

我想让UiAutomator在测试失败时截图。我目前的设置方式如下:

    @Override
protected void setUp() throws Exception {
    System.out.println("Beginning setup...");
    testFailed = true;
}

@Override
protected void tearDown() throws Exception {
    if(testFailed)
        getUiDevice().takeScreenshot("/sdcard/Pictures/Screenshots/failure.jpg");
}
然后单独的测试是这样工作的

public void testExample(){
  /* do stuff */
  testFailed = false;
}
其思想是,如果抛出异常,testFailed永远不会设置为false,并且在测试后调用它时,tearDown会截图

我真的希望能够在代码中添加testListener或未捕获的异常处理程序,从而为这个问题提供更优雅的解决方案。但是,UiAutomator有自己的未捕获异常处理程序,它覆盖了我试图实现的异常处理程序。我可以添加未捕获的异常处理程序,但它只是被忽略了

是否有任何方法可以添加一个未捕获的异常处理程序,在抛出异常的位置和UiAutomator的未捕获异常处理程序之间拦截异常,然后将它们传递给UiAutomator


是否有其他方法可以侦听测试失败/抛出异常,而无需在每个测试结束时专门添加该布尔值?

而不是调用getUiDevice()。teardown中的takeScreenshot()将其放入函数中-比如foo(),并在异常发生时调用它

现在将要测试的代码放在try-catch块和catch-call foo()中。有点像这样-

try{
//add your test code here
}
catch(Exception e){
foo();
//and if you want to throw exception again so your current test flow is not disturbed then add
throw e;
}

很好,希瓦姆·古普塔。。我正要提出同样的建议!!你是个救生员。谢谢:-)