How can we access context of an application in Robolectric?
RobolectricRobolectric Problem Overview
Actually, I need to get a response of an API call, for that I required Context
.
Robolectric Solutions
Solution 1 - Robolectric
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()
Solution 2 - Robolectric
You can use
RuntimeEnvironment.application
Solution 3 - Robolectric
Add
testImplementation "androidx.test:core-ktx:${deps.testrunner}"
And use:
private val app = ApplicationProvider.getApplicationContext()
Solution 4 - Robolectric
Use this:
Robolectric.application
Solution 5 - Robolectric
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.
Solution 6 - Robolectric
To get application context you must do the following:
- annotate @RunWith(RobolectricTestRunner.class)
- RuntimeEnvironment.application.getApplicationContext()
Solution 7 - Robolectric
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$
Solution 8 - Robolectric
Agree with answers of @EugenMartynov and @rds ....
A quick example can be found at Volley-Marshmallow-Release
in NetworkImageViewTest.java
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 :
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.robolectric:robolectric:3.1.2'
}```
Solution 9 - Robolectric
This works for me with Robolectric 3.5.1: ShadowApplication.getInstance().applicationContext
Solution 10 - Robolectric
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.)
Solution 11 - Robolectric
First add the following to your build.gradle
:
testImplementation 'androidx.test:core:1.2.0'
then use:
ApplicationProvider.getApplicationContext() as Application
Solution 12 - Robolectric
It is safer to use Robolectric.getShadowApplication()
instead of using Robolectric.application
directly.
Solution 13 - Robolectric
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)
Solution 14 - Robolectric
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