How can we access context of an application in Robolectric?

℡╲_俬逩灬. 提交于 2019-11-27 05:41:23

问题


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


回答1:


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()
    



回答2:


You can use

RuntimeEnvironment.application



回答3:


Use this:

Robolectric.application



回答4:


Add

testImplementation "androidx.test:core-ktx:${deps.testrunner}"

And use:

private val app = ApplicationProvider.getApplicationContext()



回答5:


To get application context you must do the following:

  1. annotate @RunWith(RobolectricTestRunner.class)
  2. RuntimeEnvironment.application.getApplicationContext()



回答6:


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.




回答7:


This works for me with Robolectric 3.5.1: ShadowApplication.getInstance().applicationContext




回答8:


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.)




回答9:


It is safer to use Robolectric.getShadowApplication() instead of using Robolectric.application directly.




回答10:


Agree with answers of @EugenMartynov and @rds ....

A quick example can be found at Volley-Marshmallow-Release

in NetworkImageViewTest.java

// mNIV = new NetworkImageView(Robolectric.application); mNIV = new NetworkImageView(RuntimeEnvironment.application);

Volley link is available https://android.googlesource.com/platform/frameworks/volley/+/marshmallow-release

you have to add dependencies in volley module in android studio as :

dependencies { testCompile 'junit:junit:4.12' testCompile 'org.mockito:mockito-core:1.10.19' testCompile 'org.robolectric:robolectric:3.1.2' }




回答11:


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)




回答12:


Ok, so I know many others said this answer before and might already outdated

    when(mockApplication.getApplicationContext()).thenReturn(RuntimeEnvironment.application);
    when(mockApplication.getFilesDir()).thenReturn(RuntimeEnvironment.application.getFilesDir());

    sharedPref = RuntimeEnvironment.application.getSharedPreferences(KEY_MY_PREF, Context.MODE_PRIVATE);
    sut = new BundleManagerImpl(mockApplication,
            processHtmlBundle, resultListener, sharedPref);

I got null, because the when() part was AFTER the sut initialization. It might help some of you.

also I have the

@RunWith(CustomRobolectricTestRunner.class)
@Config(constants = BuildConfig.class)

at the beginning of the class

Also

 when(mockApplication.getApplicationContext()).thenReturn(RuntimeEnvironment.application.getApplicationContext()); works



回答13:


First add the following to your build.gradle:

testImplementation 'androidx.test:core:1.2.0'

then use:

ApplicationProvider.getApplicationContext() as Application




回答14:


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$



来源:https://stackoverflow.com/questions/13684094/how-can-we-access-context-of-an-application-in-robolectric

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!