不幸的是,当我运行我的 AndroidJunit4 测试时,我无法克服以下错误:
测试运行失败:找不到仪器信息:ComponentInfo{com.fisincorporated.aviationweather.test/android.support.test.runner.AndroidJUnitRunner}
我尝试了各种不依赖于 Android Studio 版本的 SO 解决方案,但没有运气
我的应用级 gradle 代码片段是:
android {
...
defaultConfig {
...
testInstrumentationRunner "com.fisincorporated.aviationweather.app.OverrideApplicationTestRunner"
}
...
}
我的 OverrideApplicationTestRunner 是:
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);
}
}
和 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 目录结构如下:
我发现如果我运行以下测试,它就可以工作,即我看到 WeatherApplicationTest 正在执行。所以似乎 testInstrumentationRunner 正在寻找我的 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());
}
}
那么问题是由使用 ActivityTestRule 引起的吗?
PS我忘了说我是测试新手,所以如果我遗漏了一些明显的东西,我深表歉意。