2015-12-11 56 views
0

例外,我有简单的应用程序类:Android的测试抛出应用类

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

“为什么我得到了我的异常测试开始了吗?“ - 假设你在清单中注册了这个'Application'子类(''元素中的'android:name'),那么每当你的应用程序的进程启动时,就会创建一个'Application'子类的实例。 “我的测试如何与应用程序类相关?” - 显然,你正在测试一个使用这个'Application'类的Android应用程序,正如我在前一段中所描述的那样。如果您认为情况并非如此,那么您可能会发布相关清单和测试代码。 – CommonsWare

+0

谢谢。我如何防止仅在我的测试中运行Application子类?请参阅更新。 – Dem0n13

+0

“我如何防止仅在我的测试中运行应用程序子类?” - 你没有。您修复'Application'子类,以便它在测试时可以工作。 – CommonsWare

回答

1

了一个选项,用于测试覆盖应用程序类。在配置的gradle

  1. 使用自定义testInstrumentationRunner:

    testInstrumentationRunner 'com.xxx.test.CustomTestRunner' 
    
  2. 实施亚军不使用你的应用程序类:

    public class CustomTestRunner extends InstrumentationTestRunner { 
        @Override 
        public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException { 
         return super.newApplication(cl, Application.class.getName(), context); 
        } 
    } 
    
0

这是因为你被代码应用的建筑工扔

删除

throw new Exception(); 

从MainApplication建筑工

相关问题