简体   繁体   English

如何在AndroidTestCase中获取活动或应用程序上下文

[英]How to get the activity or application context in AndroidTestCase

I am writing some test case with AndroidTestCase. 我正在用AndroidTestCase编写一些测试用例。 I want call getCacheDir to get the directory. 我想调用getCacheDir来获取目录。

/data/data/com.mypackage/cache

But I have not start up the activity or application. 但是我还没有启动活动或应用程序。 it there any way I can get the activity or application context in AndroidTestCase. 它可以以任何方式在AndroidTestCase中获取活动或应用程序上下文。

您可以在AndroidTestCase的子类中简单地重写getContext()

Since you are extending the AndroidTestCase, you can just use the call getContext() to retrieve the context. 由于您正在扩展AndroidTestCase,因此只需使用调用getContext()即可检索上下文。 AndroidTestCase implements it for you. AndroidTestCase为您实现了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当被测类不是活动时,获取上下文以在AndroidTestCase中使用 - Getting a Context for use in AndroidTestCase when class under test is not an activity 从AndroidTestCase调用活动 - Calling an activity from an AndroidTestCase 如何从非活动和finish();获取应用程序上下文? - how to get application context from non activity and finish();? 如何获取模块中没有应用程序类的非活动类的上下文 - How to get context of a non activity class with no Application Class in a module 如何在SystemUI类中获取当前活动的当前视图或应用程序上下文 - How to get Current View or Application Context of Current Activity in a class of SystemUI 如果上下文不是活动上下文而是应用程序上下文,如何启动Intent - How to start an Intent if context is not Activity Context but Application Context Forms.Context 已过时,所以我应该如何获取我的单个活动应用程序的活动? - Forms.Context is obsolete so how should I get Activity of my single activity application? 如何获取我的活动背景? - How to get my activity context? 如何获取调用者活动的上下文 - how to get the context of the caller activity 如何从AndroidTestCase中的资源获取字符串? - How do I get a String from resources in AndroidTestCase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM