我们如何在 Robolectric 中访问应用程序的上下文? [英] How can we access context of an application in Robolectric?
本文介绍了我们如何在 Robolectric 中访问应用程序的上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实际上,我需要获得 API 调用的响应,为此我需要 Context
.
Actually, I need to get a response of an API call, for that I required Context
.
推荐答案
更新.
仅用于 1.x 和 2.x 版:
Just use for version 1.x and 2.x:
Robolectric.application;
对于 3.x 版:
RuntimeEnvironment.application;
对于 4.x 版:
添加到您的
build.gradle
文件:
testImplementation 'androidx.test:core:1.0.0'
检索上下文:
retrieve the context with:
ApplicationProvider.getApplicationContext()
这篇关于我们如何在 Robolectric 中访问应用程序的上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文