How can we access context of an application in Robolectric?

后端 未结 14 1517
既然无缘
既然无缘 2020-12-05 03:32

Actually, I need to get a response of an API call, for that I required Context.

14条回答
  •  清歌不尽
    2020-12-05 04:21

    As of release 4.0-alpha-3 on July 21, they removed ShadowApplication.getApplicationContext(). Stick with RuntimeEnvironment.application.getApplicationContext() for any tests annotated with @RunWith(RobolectricTestRunner::class).

    As an aside, their current guide has an example of getting string resources using:

    final Context context = RuntimeEnvironment.application;
    

    (Note that the javadocs for RuntimeEnvironment and ShadowApplication currently reflect the non-alpha 3.x release.)

提交回复
热议问题