Android测试在Application类中抛出异常

时间:2015-12-11 23:18:22

标签: android testing android-studio

我有简单的Application类:

public class MainApplication extends Application {
    public MainApplication() throws Exception {
        throw new Exception();
    }
}

和简单测试:

public class SimpleTests extends AndroidTestCase {
    public void testSum() {
        assertEquals(4, 2 + 2);
    }
}

为什么我的测试开始出现异常?我的测试如何与Application类相关?

Running tests
Test running startedTest running failed: Instrumentation run failed due to 'java.lang.Exception'
Empty test suite.

Android Studio v.1.5.1

UPDATE :我的应用程序子类当然更复杂,并且在方法" onCreate"中包含业务逻辑。我在运行测试时不想运行这个逻辑。

2 个答案:

答案 0 :(得分:1)

选项覆盖测试的应用程序类。

  1. 在gradle配置中使用自定义testInstrumentationRunner:

    [^...]
  2. 实现不使用Application类的Runner:

    testInstrumentationRunner 'com.xxx.test.CustomTestRunner'
    

答案 1 :(得分:0)

这是因为您在Application

的构造函数中通过代码抛出它

删除

throw new Exception();

来自MainApplication constructer