不幸的是,当我运行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会导致问题吗?
P.S。我没有说我是测试的新手,所以如果我遗漏了一些明显的东西,我会道歉。
答案 0 :(得分:0)
将Android插件版本切换为2.2.3
,然后返回2.3.0
,清理项目并构建修复问题。