How can we access context of an application in Robolectric?

后端 未结 14 1505
既然无缘
既然无缘 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:15

    Update.

    Just use for version 1.x and 2.x:

    Robolectric.application;
    

    And for version 3.x:

    RuntimeEnvironment.application;
    

    And for version 4.x:

    • add to your build.gradle file:

      testImplementation 'androidx.test:core:1.0.0'
      
    • retrieve the context with:

      ApplicationProvider.getApplicationContext()
      

提交回复
热议问题