133

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

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
user1667968
  • 1,489
  • 3
  • 12
  • 16

14 Answers14

243

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()
    
Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
27

You can use

RuntimeEnvironment.application
rds
  • 26,253
  • 19
  • 107
  • 134
  • 4
    in RoboElectric 3.0, Roboelectric.application no longer exists, so this is probably the best answer – kenyee Jul 27 '16 at 19:27
20

Add

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

And use:

private val app = ApplicationProvider.getApplicationContext()
Gabor
  • 7,352
  • 4
  • 35
  • 56
John
  • 1,139
  • 3
  • 16
  • 33
19

Use this:

Robolectric.application
Xian
  • 934
  • 1
  • 7
  • 9
8

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.

The_Martian
  • 3,684
  • 5
  • 33
  • 61
6

To get application context you must do the following:

  1. annotate @RunWith(RobolectricTestRunner.class)
  2. RuntimeEnvironment.application.getApplicationContext()
user1390616
  • 171
  • 1
  • 5
3

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$ enter image description here enter image description here

Kirill Karmazin
  • 6,256
  • 2
  • 54
  • 42
2

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

Bhuro
  • 348
  • 4
  • 22
2

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

Farrukh Najmi
  • 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
2

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

qix
  • 7,228
  • 1
  • 55
  • 65
2

First add the following to your build.gradle:

testImplementation 'androidx.test:core:1.2.0'

then use:

ApplicationProvider.getApplicationContext() as Application

dougie
  • 53
  • 8
1

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

Cassie
  • 5,223
  • 3
  • 22
  • 34
1

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)

grego
  • 480
  • 6
  • 8
0

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
narancs
  • 5,234
  • 4
  • 41
  • 60