Actually, I need to get a response of an API call, for that I required Context
.
It is safer to use Robolectric.getShadowApplication()
instead of using Robolectric.application
directly.
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