问题
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