Android 单元测试/test.runner - 访问 Application 对象
我正在使用 android.test.runner 库和 AndroidTestCase 等来创建一些单元测试。我发现它很有用,但一项测试需要访问应用程序对象。通常我只是从活动上下文中获取它,例如
AppState appState = ((AppState) myActivity.getApplicationContext());
但是,单元测试位于扩展 AndroidTextCase 的类中,据我所知,没有可用的 getApplicationContext 。有一个 getContext,但我不清楚这是否是我想要的。最好的做法是什么?
I'm using the android.test.runner library and the AndroidTestCase etc. to create some unit tests. I'm finding it useful, but one test requires access to the application object. Usually I just get that from the activity context, e.g.
AppState appState = ((AppState) myActivity.getApplicationContext());
However, the unit tests are in a class which extends AndroidTextCase, and as far as I can see there is no getApplicationContext available. There is a getContext, but I'm not clear if that's what I want. What's the best course of action?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否正在测试您的 AppState 对象,或者尝试测试依赖于它的其他东西?
如果您不在此实例中测试 AppState,您可以使用 Mockito 等工具来模拟 AppState 对象以用于测试目的。
Are you testing your AppState object, or trying to test something else which relies on it?
If you are not testing AppState in this instance can you use a tool such as Mockito to mock up an AppState object for the purposes of your test.