How can we access context of an application in Robolectric?

落花浮王杯 提交于 2019-11-28 06:39:39

Update.

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

Robolectric.application;

And for version 3.x:

RuntimeEnvironment.application;

You can use

RuntimeEnvironment.application

Use this:

Robolectric.application
John

Add

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

And use:

private val app = ApplicationProvider.getApplicationContext()

To get application context you must do the following:

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

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.

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

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

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

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' }

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)

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

First add the following to your build.gradle:

testImplementation 'androidx.test:core:1.2.0'

then use:

ApplicationProvider.getApplicationContext() as Application

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