如何prevent ActivityUnitTestCase从主叫Application.onCreate? [英] How to prevent ActivityUnitTestCase from calling Application.onCreate?

查看:116
本文介绍了如何prevent ActivityUnitTestCase从主叫Application.onCreate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定是失去了一些东西。的Javadoc, ActivityUnitTestCase 显示,这个测试用例测试从系统中隔离的活动:

I must be missing something here. The JavaDoc of ActivityUnitTestCase suggest that this test case tests an Activity in isolation from the system:

这个类提供了一个单一的活动隔离测试。测试活动将在系统中最基本的连接被创建,你可以注入嘲笑或wrappered许多活动的依赖版本。

This class provides isolated testing of a single activity. The activity under test will be created with minimal connection to the system infrastructure, and you can inject mocked or wrappered versions of many of Activity's dependencies.

我是假设,其中包括没有真正启动应用程序。此外,它暴露了一个 setApplication 帮手,人们可以presumably用于注入模拟应用程序。

I was assuming that includes not actually starting the application. Moreover, it exposes a setApplication helper that one can presumably use to inject a mock application.

然而,任何 ActivityUnitTestCase 我开始启动(实际)的应用程序,并调用它的的onCreate 方法。更多precisely,在 InstrumentationTestRunner 似乎是在做了,而且这样做之前,我得到一个机会, setApplication 在我的测试的设置的方法!我甚至没有注意到,相当长一段时间,因为它似乎发生在那里甚至没有Eclipse的断点到达时测试套件推出了点,但写在日志中的onCreate 显示,它实际上调用。

However, any ActivityUnitTestCase I start launches the (actual) application and calls its onCreate method. More precisely, the InstrumentationTestRunner seems to be doing that, and doing so even before I get a chance to setApplication in my test's setUp method! I didn't even notice that for quite a while, since it seems to happen at a point during test suite launch where not even Eclipse breakpoints are reached, but writing to the logs in onCreate reveals that it's actually called.

这是完全超越了我。我为什么要使用一个模拟的应用程序对象时,Android的测试运行实例反正执行的实际应用?这是更成问题考虑到仪器亚军在自己的线程中运行,这样当产卵主应用程序线程。这意味着在测试执行和 Application.onCreate 被称为之间的竞争条件。如果你做任何事情的存在,可能会影响你的测试,例如:写入共享preference文件,那么你就彻底完蛋了,因为你的测试会随机失败。

This is completely beyond me. Why would I want to use a mock app object when Android's test runner instantiates and executes the actual application anyway? This is even more problematic considering that the instrumentation runner runs in its own thread, and spawns the main application thread when doing so. This means there is a race condition between the test being executed and Application.onCreate being called. If you do anything in there that could affect your tests, e.g. writing to a shared preference file, then you're completely screwed, since your tests will randomly fail.

我缺少的东西或者这只是在测试框架的严重疏忽?

Am I missing something or is this simply a gross oversight in the test framework?

更新 这似乎影响到 ApplicationTestCase 为好。在我的测试情况下甚至开始,我可以在我的应用类的onCreate 到达一个断点。我们开始发射后忘记在那里的AsyncTask,它会随机失败,因为我没有得到任何机会去嘲笑它(请记住,这是安装程序之前被称为我的测试用例)。这里是堆栈跟踪的onCreate这个不起眼的调用过程中我明白了:

UPDATE This seems to affect ApplicationTestCase as well. Before my test case is even started, I can reach a breakpoint in my application class' onCreate. We start a fire-and-forget AsyncTask in there, which will randomly fail because I get no chance to mock it out (remember, that's before setUp is called on my test case). Here is the stack trace I see during this obscure invocation of onCreate:

Thread [<1> main] (Suspended (breakpoint at line 86 in QypeRadar))  
QypeRadar.onCreate() line: 86   
InstrumentationTestRunner(Instrumentation).callApplicationOnCreate(Application) line: 969   
ActivityThread.handleBindApplication(ActivityThread$AppBindData) line: 4244 
ActivityThread.access$3000(ActivityThread, ActivityThread$AppBindData) line: 125    
ActivityThread$H.handleMessage(Message) line: 2071  
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 123 
ActivityThread.main(String[]) line: 4627    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 521  
ZygoteInit$MethodAndArgsCaller.run() line: 868  
ZygoteInit.main(String[]) line: 626 
NativeStart.main(String[]) line: not available [native method]  

为什么测试运行 callApplicationOnCreate 尽管该文档明确规定:

测试用例不会叫的onCreate(),直到你的测试调用createApplication()。这给你一个机会来设置或调整的onCreate()之前,任何额外的框架或测试逻辑。

The test case will not call onCreate() until your test calls createApplication(). This gives you a chance to set up or adjust any additional framework or test logic before onCreate().

这是一个全力以赴的谎言 - 它不给我机会。

That's a flat out lie--it doesn't give me the chance!

推荐答案

Roboguice 有同样的问题。检查它这里

Roboguice had the same issue. Check it here.

这篇关于如何prevent ActivityUnitTestCase从主叫Application.onCreate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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