简体   繁体   English

Android JUnit测试应用程序类

[英]Android JUnit testing Application class

I am trying to setup JUnit test cases for an app I am developing currently. 我正在尝试为当前正在开发的应用设置JUnit测试用例。 The main problem I am facing is that I am not able to get access of an object which I am storing in my Application class. 我面临的主要问题是我无法访问要存储在Application类中的对象。 I am extending the AndroidTestCase class for writing test cases. 我正在扩展用于编写测试用例的AndroidTestCase类。

I tried getting access to that object(say myData) by 我试图通过以下方式访问该对象(例如myData)

Myapp app = (Myapp) getContext()
                .getApplicationContext();
String myData=app.getData();

But this is throwing null pointer exception at app.getData(); 但这会在app.getData()处引发空指针异常; how can i access this object at run time? 我如何在运行时访问该对象?

Please, show more code, class where extends AndroidTestCase , class where extends TestSuite 请显示更多代码, extends AndroidTestCase类, extends TestSuite

Also, you may forget add to AndroidManifest : 另外,您可能会忘记添加到AndroidManifest中

...

  <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="YOUR_PACKAGE"/>

    <application
        android:label="@string/app_name"
        android:name="YOUR_APPLICATION_CLASS">

        <uses-library android:name="android.test.runner"/>

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM