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

- 19,513
- 22
- 110
- 155

- 1,489
- 3
- 12
- 16
14 Answers
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()

- 11,839
- 12
- 77
- 125

- 19,888
- 10
- 61
- 114
-
12Did you put @RunWith(RobolectricTestRunner.class) for your tests? – Eugen Martynov Dec 04 '12 at 09:25
-
4Yeah.. I added... but still it return null – user1667968 Dec 04 '12 at 09:55
-
One has to use the build variant "unit tests" too to make Robolectric tests working. This could be why RuntimeEnvironment.application is null in previous comments – Mat Jul 20 '15 at 06:18
-
1I have done all of what you've mentioned and still get null. Anything else I might be missing? – Moises Jimenez Sep 23 '15 at 15:22
-
`@RunWith` should be enough – Eugen Martynov Sep 23 '15 at 15:43
-
15Also make sure that you don't use `RuntimeEnvironment.application` in static code (like methods annotated with `@BeforeClass`) as Robolectric will probably not be initialized at that point and the value will be `null`. – sfera Dec 07 '15 at 14:41
-
1Also this causes the application to bleed between tests .. which may not be desirable – Chris May 04 '16 at 22:04
-
And if it is still null ? – ilbets Feb 16 '17 at 18:11
-
Then you probably forgot to use `RobolectricTestRunner` – Eugen Martynov Feb 16 '17 at 19:37
-
RuntimeEnvironment.application does not seem to have access to the resources of the application – fobbymaster Mar 08 '17 at 20:13
-
What kind of resources? – Eugen Martynov Mar 09 '17 at 07:59
-
This works for me with Robolectric 3.5.1: `ShadowApplication.getInstance().applicationContext` – Farrukh Najmi Jan 10 '18 at 23:19
-
Actually there is no code example added for the question, but I guess that you are trying to access the context inside one of your under-test classes, and apparently the context which you try to access is not the one from Robolectric. Please make sure that you are using the same context from `RuntimeEnvironment.application` in all of your classes which you are testing and if the problem still exists, please update the question with code examples. – superus8r Aug 21 '18 at 10:36
-
Why testImplementation and not androidTestImplementation? – Trevor Mar 24 '23 at 06:05
-
@Trevor at the time writing it the Robolectic was used only for running tests on JVM – Eugen Martynov Mar 24 '23 at 10:47
You can use
RuntimeEnvironment.application

- 26,253
- 19
- 107
- 134
-
4in RoboElectric 3.0, Roboelectric.application no longer exists, so this is probably the best answer – kenyee Jul 27 '16 at 19:27
Add
testImplementation "androidx.test:core-ktx:${deps.testrunner}"
And use:
private val app = ApplicationProvider.getApplicationContext()
-
-
2
-
3This is the correct answer with the latest Robolectric. Other methods mentioned here are deprecated or removed. – Gabor May 21 '19 at 01:00
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.

- 3,684
- 5
- 33
- 61
To get application context you must do the following:
- annotate @RunWith(RobolectricTestRunner.class)
- RuntimeEnvironment.application.getApplicationContext()

- 171
- 1
- 5
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$

- 6,256
- 2
- 54
- 42
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'
}

- 348
- 4
- 22
This works for me with Robolectric 3.5.1: ShadowApplication.getInstance().applicationContext

- 5,055
- 3
- 35
- 54
-
Note that it looks like the 4.0 release will remove this method; better to stick with `RuntimeEnvironment.application` or `RuntimeEnvironment.application.getApplicationContext()` if its working for you. – qix Aug 14 '18 at 07:56
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.)

- 7,228
- 1
- 55
- 65
First add the following to your build.gradle
:
testImplementation 'androidx.test:core:1.2.0'
then use:
ApplicationProvider.getApplicationContext() as Application

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

- 5,223
- 3
- 22
- 34
-
But what if I need to access some custom properties of my custom application? It seems that I can't get the real object from shadow application. – Denys Kniazhev-Support Ukraine Jan 12 '15 at 12:46
-
@DenisKniazhev Sorry I can't answer that for you. Shortly after we started using Robolectric we started using Travis as our CI, and they don't play nicely. My guess is that you can either cast it to your application or you might need to make a custom runner to get access to it that way. Best of luck – Cassie Jan 13 '15 at 22:20
-
Thanks, for now I have just stuck to `Robolectric.application` – Denys Kniazhev-Support Ukraine Jan 15 '15 at 06:28
-
8
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)

- 480
- 6
- 8
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

- 5,234
- 4
- 41
- 60