开发者

Android JUnit: Define a different Application subclass

开发者 https://www.devze.com 2023-03-01 16:26 出处:网络
So for my normal Android project, I have the following in AndroidManifest.xml: <application android:name=\".utilities.App\" ...>

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

<application android:name=".utilities.App" ...>
    开发者_JS百科 ....
</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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号