Android JUnit: Define a different Application subclass

。_饼干妹妹 提交于 2019-12-22 07:01:16

问题


So for my normal Android project, I have the following in AndroidManifest.xml:

<application android:name=".utilities.App" ...>
     ....
</application>

And then I have my App class:

public class App extends Application {
     ....
}

And then I have an Android JUnit Test project associated with the Android project. Everything is all fine and dandy and I can write JUnit tests. However, I'm trying to run code coverage with my JUnit tests and I'm getting bloated results. The reason is because my App class gets called and initialized as if my application were actually started. I do not want my custom App class to execute when I run the JUnit tests or code coverage. Any setup I would need for the JUnit tests will go in the appropriate JUnit setup() method. Is there any way I can prevent it from executing my custom App class or a way that any classes/methods/lines that are executed due to the creation of my App class aren't counted towards the code coverage?


回答1:


A temporary solution that I've found will work unless someone has any better ideas.

  • Go into the main Android project's AndroidManifest.xml.
  • Change the android:name attribute from ".utilities.App" to "android.app.Application"
  • Run the code coverage utility/JUnit tests
  • Change the android:name attribute back from "android.app.Application" to ".utilities.App"
  • Re-deploy the app onto the device (so that it uses the right Application class when it runs external to the code coverage/JUnit tests)

I'm sure the real solution is to automate this process, but I'm too lazy to do so, and it just feels hackish and wrong. But at least it's a workaround unless someone has any ideas.



来源:https://stackoverflow.com/questions/5827139/android-junit-define-a-different-application-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!