Android JUnit: defining another application subclass

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

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

And then I have an App class:

 public class App extends Application { .... } 

And then I have an Android JUnit Test project related to an Android project. Everything is fine and dandy, and I can write JUnit tests. However, I try to run code coverage using JUnit tests, and I get bloated results. The reason is because my application class is being called and initialized as if my application was really running. I don't want my custom App class to run when running JUnit tests or code coverage. Any configuration I need for JUnit tests will go through the appropriate JUnit setup() method. Is there any way to prevent it from executing my own application class or the way that any classes / methods / lines that are executed due to the creation of my application class are not taken into account to cover the code?

+4
source share
1 answer

The workaround I found will work if someone doesn't have the best ideas.

  • Go to the main Android project AndroidManifest.xml.
  • Change the android: name attribute from ".utilities.App" to "android.app.Application"
  • Run JUnit Code Coverage / Tests Utility
  • Change the android: name attribute back from "android.app.Application" to ".utilities.App"
  • Redeploy the application on the device (so that it uses the correct application class when it runs external code testing / JUnit tests)

I'm sure the real solution is to automate this process, but I'm too lazy to do this and it just feels hacked and wrong. But at least this is a workaround if someone has no ideas.

0
source

All Articles