Android - 在Instrumentation测试期间捕获RuntimeException

时间:2016-05-11 14:59:32

标签: java android exception instrumentation android-instrumentation

我正在尝试为小型Android应用程序的某些活动设置一些检测测试。我已成功设置了一些测试类,但我遇到了一个问题:我似乎无法从我的测试方法中捕获任何异常。

目前,我有一个Activity需要设置一些参数,如果没有设置这些参数,则会抛出RuntimeException。 我已经定义了以下测试类,以确保在没有设置任何内容时Activity崩溃:

public class DeviceWithoutMetadata extends ActivityInstrumentationTestCase2<DeviceActivity_>
{
    public DeviceWithoutMetadata()
    {
        super(DeviceActivity_.class);
    }

    public void testCrashBecauseManifestPoorlyConfigured()
    {
        try
        {
            Logger.i("Beginning crash....");
            this.getActivity();
            Logger.i("Ending crash....");
        }
        catch (Throwable nmdre)
        {
            Logger.i("Absolutely normal to catch something here.");
        }
    }
}

如果我没有弄错,this.getActivity()会启动活动,这会导致RuntimeException被抛出(我希望我的测试告诉我&#34;那&#39} ;好吧,我按预期获得了RuntimeException&#34;)。问题是,我可以看到我的&#34;开始崩溃&#34;记录,但不是&#34;结束崩溃&#34;也不是#34;绝对正常......&#34;日志。相反,我的测试失败了,告诉我以下(减轻)堆栈跟踪:

MonitoringInstrumentation: Exception encountered by: com.axible.axcore.activities.DeviceActivity_@305a0bd. Dumping thread state to outputs and pining for the fjords.
com.axible.axcore.exception.runtime.NoMetadataDefinedRuntimeException: The calling activity must define a <meta-data android:name="validation_scan_classname"> tag, that must contain the name of the class which will validate the scan of the Device ID.
at com.axible.axcore.activities.DeviceActivity.afterInject(DeviceActivity.java:36)
at com.axible.axcore.activities.DeviceActivity_.init_(DeviceActivity_.java:48)
at com.axible.axcore.activities.DeviceActivity_.onCreate(DeviceActivity_.java:38)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)

我试图抓住特定的例外(NoMetadataDefinedRuntimeException,其中RuntimeException扩展,然后RuntimeException,然后Exception,然后Throwable ......我无法抓住任何东西。

如何从仪器测试中捕获此异常?

0 个答案:

没有答案