【问题标题】:JUnit TestWatcher : failed, is it possible to remove the thrown exception (manipulating Throwable/stacktrace)?JUnit TestWatcher:失败,是否可以删除抛出的异常(操作 Throwable/stacktrace)?
【发布时间】:2016-05-25 17:17:55
【问题描述】:

在 JUnit 中,使用 TestWatcher 并重写 failed() 函数,是否可以删除抛出的异常并改为自己断言?

用例是:在 Android 上进行功能测试,当测试导致应用崩溃时,我想将 NoSuchElementException 替换为 AssertionError ("app crashed ")。

自定义断言没有问题(当我在 finished() 方法中检测到崩溃时),但是如何删除抛出的异常?

因为在我的报告中它为一个测试创建了异常和断言,所以失败的次数比失败中的测试多,这是合乎逻辑的,但很烦人。

我想知道是否有一种方法可以自定义 Throwable 对象以删除特定的 NoSuchElementException,从而操纵堆栈跟踪。

我没能做到。 (而且我一定不想在每个测试中都使用 try/catch 来执行它......)。

【问题讨论】:

    标签: java android junit try-catch throwable


    【解决方案1】:

    您可以覆盖TestWatcher.apply 并为NoSuchElementException 添加一个特殊的catch

    public class MyTestWatcher extends TestWatcher {
        public Statement apply(final Statement base, final Description description) {
            return new Statement() {
                @Override
                public void evaluate() throws Throwable {
                    List<Throwable> errors = new ArrayList<Throwable>();
    
                    startingQuietly(description, errors);
                    try {
                        base.evaluate();
                        succeededQuietly(description, errors);
                    }
                    catch (NoSuchElementException e) {
                        // ignore this
                    }
                    catch (AssumptionViolatedException  e) {
                        errors.add(e);
                        skippedQuietly(e, description, errors);
                    }
                    catch (Throwable e) {
                        errors.add(e);
                        failedQuietly(e, description, errors);
                    }
                    finally {
                        finishedQuietly(description, errors);
                    }
    
                    MultipleFailureException.assertEmpty(errors);
                }
            };
        }
    

    【讨论】:

      【解决方案2】:

      你可以通过绕过来做到这一点。下面给出了一个示例代码。希望对你有帮助。

      try {
      // Write your code which throws exception
      ----
      ----
      ----
      
      } catch (NoSuchElementException ex) {
          ex.printStackTrace();
          if (ex instanceof NoSuchElementException) { // bypass
                                                      // NoSuchElementException
              // You can again call the method and make a counter for deadlock
              // situation or implement your own code according to your
              // situation
              AssertionError ("app crashed");
              if (retry) {
                  ---
                  ---
                  return previousMethod(arg1, arg2,...);
              } else {
                  throw ex;
              }
          }
      } catch (final Exception e) {
          e.printStackTrace();
          throw e;
      }
      

      我之前已经解决了这类问题。我的另一个答案有详细信息。你可以通过我的另一个答案:android.security.KeyStoreException: Invalid key blob

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-08
        • 2018-03-02
        • 1970-01-01
        • 1970-01-01
        • 2014-05-09
        • 1970-01-01
        • 1970-01-01
        • 2018-04-24
        相关资源
        最近更新 更多