JUnit TestWatcher: failed, is it possible to remove the thrown exception (manipulating Throwable / stacktrace)?

In JUnit, using TestWatcher and overriding the failed () function , is it possible to remove the thrown exception and instead make its own statement?

Use case: with functional tests on Android, when the test crashes the application, I would like to replace NoSuchElementException with AssertionError ("application crashed" ").

I have no problem creating a custom statement (when I detect a failure in the ready () method ), but how to remove the thrown exception?

Because in my report, it creates an exception and an assertion for one test, therefore there are more failures than the test on failure, which is logical, but annoying.

I was wondering if there is a way to set up a Throwable object to remove a specific NoSuchElementException by manipulating a stacktrace.

I did not succeed. (And I certainly don’t want to execute it with try / catch in every test ...).

+4
source share
2 answers

You can override TestWatcher.applyand add special catchfor NoSuchElementException:

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);
            }
        };
    }
+3
source

You can do this by going around. The following is sample code. Hope this helps you.

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:

+1

All Articles