Android JUnit testing Application class



我正在尝试为我目前正在开发的应用程序设置 JUnit 测试用例。我面临的主要问题是我无法访问存储在应用程序类中的对象。我正在扩展AndroidTestCase类来编写测试用例。

我尝试通过以下方式访问该对象(例如myData)

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

但这是在 app.getData() 上抛出空指针异常;如何在运行时访问此对象?

请显示更多代码,类在哪里extends AndroidTestCase,类在哪里extends TestSuite

另外,您可能会忘记添加到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"/>
...

相关内容

最新更新