Android的JUnit的:定义不同的应用程序子类 [英] Android JUnit: Define a different Application subclass

查看:196
本文介绍了Android的JUnit的:定义不同的应用程序子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,对我正常的Andr​​oid项目,我有以下在AndroidManifest.xml中:

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

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

然后,我有我的App类:

And then I have my App class:

public class App extends Application {
     ....
}

然后,我与Android项目相关联的一个Android JUnit测试项目。一切都很好,很正常,我可以写JUnit测试。不过,我想用我的JUnit测试运行code覆盖和我越来越臃肿的效果。究其原因是因为我的应用程序类被调用,并就好像我的应用程序实际上是启动初始化。我不希望我的自定义应用程序类,当我运行JUnit测试或code范围执行。任何设置我需要的JUnit测试会在适当的JUnit 设置()方法。有没有什么办法可以prevent从执行我的自定义应用程序的类或一种方式,这是由于我的创作App类的执行任何类/方法/线没有计算入code覆盖?

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.

  • 进入到主Android项目的Andr​​oidManifest.xml中。
  • 更改了android:从.utilities.App到android.app.Applicationname属性
  • 运行code覆盖效用/ JUnit测试
  • 更改了android:名字从android.app.Application到.utilities.App
  • 属性回
  • 重新部署应用程序到设备(以便它使用正确的应用程序类,当它运行外部的code覆盖/ JUnit测试)

我敢肯定,真正的解决办法是将这个过程自动化,但我懒得这样做,它只是感觉hackish的和错误的。但至少这是一个解决办法,除非有人有任何想法。

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.

这篇关于Android的JUnit的:定义不同的应用程序子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆