如何解决在仪器化的单元测试中,哪种方法更好地访问上下文?
在仪器化的单元测试中,更建议以下哪一项访问Context
:使用ApplicationProvider
或InstrumentationRegistry
?
val context: Context = ApplicationProvider.getApplicationContext()
import androidx.test.platform.app.InstrumentationRegistry
val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
我知道androidx.test.InstrumentationRegistry
已过时,但androidx.test.platform.app.InstrumentationRegistry
却已过时。
解决方法
您可以根据您的SDK版本使用androidx.test.platform.app.InstrumentationRegistry或android.support.test.InstrumentationRegistry并通过以下方式获取上下文
上下文上下文= InstrumentationRegistry.getInstrumentation()。getTargetContext();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。