2013-10-21 113 views
1

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

我试图通过

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

获得访问该对象(说MYDATA的),但它在app.getData抛出空指针异常(); 如何在运行时访问此对象?

回答

2

快,让更多的代码,所在班级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"/> 

... 
+0

感谢您的答复。我没有在测试应用程序的Manifest中的应用程序标记中添加android:name =“”属性。我应该如何添加?班级名称应该是什么?假设我的目标应用有applciation class'MyApp',那么应该在测试应用 – afadfadf

+0

的清单中添加什么,我认为** android:name =“MyApp”**如果没有在包下,请尝试使用IDE的建议热键,用于Intellij Idea的ctrl +空格 – artemiygrn