Actually, I need to get a response of an API call, for that I required Context
.
You can use
RuntimeEnvironment.application
Add
testImplementation "androidx.test:core-ktx:${deps.testrunner}"
And use:
private val app = ApplicationProvider.getApplicationContext()
To get application context you must do the following:
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'
}
This works for me with Robolectric 3.5.1: ShadowApplication.getInstance().applicationContext
Use this:
Robolectric.application