I am trying to mock dependencies like what is suggested in https://artemzin.com/blog/how-to-mock-dependencies-in-unit-integration-and-functional-tests-dagger-robolectric-instrumentation/
Unfortunately I can't get past the following error when I run my AndroidJunit4 test :
Test running failed: Unable to find instrumentation info for: ComponentInfo{com.fisincorporated.aviationweather.test/android.support.test.runner.AndroidJUnitRunner}
I tried various SO solutions that were not Android Studio version dependent with no luck
My app level gradle code snippet is:
android {
...
defaultConfig {
...
testInstrumentationRunner "com.fisincorporated.aviationweather.app.OverrideApplicationTestRunner"
}
...
}
My OverrideApplicationTestRunner is:
public class OverrideApplicationTestRunner extends AndroidJUnitRunner {
@Override
@NonNull
public Application newApplication(@NonNull ClassLoader cl,
@NonNull String className,
@NonNull Context context)
throws InstantiationException,
IllegalAccessException,
ClassNotFoundException {
return Instrumentation.newApplication(WeatherApplicationTest.class, context);
}
}
WeatherApplicationTest
public class WeatherApplicationTest extends WeatherApplication {
@Override
protected void createDaggerInjections() {
component = DaggerDiComponent.builder()
.appModule(new AppModule(this) {
@Override
public String providesAppSharedPreferencesName() {
return "SHARED_AIRPORT_FUNCTIONAL_TEST";
}
public Retrofit provideAppRetrofit() {
return new AppRetrofit(new MockInterceptor()).getRetrofit();
}
})
.build();
component.inject(this);
}
}
And AirportWeatherActivityTest
@RunWith(AndroidJUnit4.class)
public class AirportWeatherActivityTest {
@Rule
public ActivityTestRule<AirportWeatherActivity> mActivityRule =
new ActivityTestRule<>(AirportWeatherActivity.class, true, false);
@Test
public void someTest() {
Context targetContext = InstrumentationRegistry.getTargetContext();
Intent intent = new Intent(targetContext, AirportWeatherActivity.class);
mActivityRule.launchActivity(intent);
assertThat(mActivityRule.getActivity().airportWeatherViewModel.airportWeatherList.size(),is(0));
}
}
androidTest directory structure is below:
I have found that if I run the following test, it works, i.e. I see that WeatherApplicationTest is being executed. So it would seem that testInstrumentationRunner is finding my OverrideApplicationTestRunner.
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.fisincorporated.aviationweather", appContext.getPackageName());
}
}
So is problem caused by using an ActivityTestRule?
P.S. I neglected to say I am a newbie on testing so my apologizes if I am missing something obvious.
Switching android plugin version to 2.2.3
and then back to 2.3.0
back, cleaning project and building fixes the issue.