How can we access context of an application in Robolectric?

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

    For the latest Robolectric 4.3 as of right now in 2019 `

    ShadowApplication.getInstance()

    ` and

    Roboletric.application

    are both depricated. So I am using

    Context context = RuntimeEnvironment.systemContext;
    

    to get Context.

    0 讨论(0)
  • 2020-12-05 04:13

    First add the following to your build.gradle:

    testImplementation 'androidx.test:core:1.2.0'

    then use:

    ApplicationProvider.getApplicationContext() as Application

    0 讨论(0)
  • 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()
      
    0 讨论(0)
  • 2020-12-05 04:17

    In your case I think you should be mindful of what it is you're actually testing. Sometimes running into issues of untestable code or seemingly untestable code is a sign that maybe your code needs to be refactored.

    For an API call response you might not want to test the API call itself. It may not be necessary to test that it's possible to send/receive information from any arbitrary web service, but rather that your code handles and processes your response in an expected manor.

    In which case it might be better to refactor the code you're trying to test. Break out the response parsing/handling to another class which accepts a simple String and do your testing on that class by injecting sample string responses.

    This is more or less following the ideas of Single Responsibility and Dependency Inversion (The S and D in SOLID)

    0 讨论(0)
  • 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.)

    0 讨论(0)
  • 2020-12-05 04:22

    In some cases, you may need your app's context instead of the Robolectris default context. For example, if you want to get your package name. By default Robolectric will return you org.robolectric.default package name. To get your real package name do the following:

    build.gradle

    testImplementation 'org.robolectric:robolectric:4.2.1'

    Your test class:

    @RunWith(RobolectricTestRunner.class)
    @Config( manifest="AndroidManifest.xml")
    public class FooTest {
    
    @Test
    public void fooTestWithPackageName(){
        Context context = ApplicationProvider.getApplicationContext();
        System.out.println("My Real Package Name: " + context.getPackageName());
    }
    
    }
    

    Make sure that in your Run/Debug Configurations Working directory is set to: $MODULE_DIR$

    0 讨论(0)
提交回复
热议问题