我正在尝试运行 UI 测试。当我不模拟任何注入(inject)的依赖项时,一切都运行良好。当我将模块的 @Provides 返回到模拟对象时,我收到以下消息:
Started running tests
Test instrumentation process crashed.
Check com.nerfy.features.core.CoreTest#a_shouldDisplayLocationPermissionRequestDialogAtStartup.txt for details.
Tests ran to completion.
我在任何地方都找不到这个 txt 文件,只有一些测试 xml,根本没有任何信息。
编辑:该文件仅包含:INSTRUMENTATION_RESULT:shortMsg=进程崩溃。 INSTRUMENTATION_CODE:0。
应用程序在其他方面运行良好。
应用程序代码:
public class Nerfy extends Application {
private AppComponent appComponent;
public static Nerfy get(Context context) {
return (Nerfy) context.getApplicationContext();
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("on create");
if (appComponent == null) {
appComponent = buildProdAppComponent();
appComponent.inject(this);
}
}
public AppComponent buildProdAppComponent() {
return DaggerAppComponent.builder()
.appModule(new AppModule(this))
.navigationModule(new NavigationModule())
.refreshModule(new RefreshModule())
.repoModule(new RepoModule())
.sharedDataModule(new SharedDataModule())
.threadsModule(new ThreadsModule())
.build();
}
//@VisibleForTesting
public void setAppComponent(AppComponent appComponent) {
System.out.println("on set");
this.appComponent = appComponent;
this.appComponent.inject(this);
}
public AppComponent getAppComponent() {
return appComponent;
}
}
这是一些测试代码:
@RunWith(AndroidJUnit4.class)
@LargeTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class CoreTest {
private UiDevice device;
public SharedData sharedData;
private AppComponent espressoAppComponent;
@Rule
public ActivityTestRule<Core> mActivityRule =
new ActivityTestRule<Core>(Core.class) {
@Override
protected void beforeActivityLaunched() {
System.out.println("beforeActivityLaunched");
sharedData = mock(SharedData.class);
System.out.println("shared data " + sharedData);
espressoAppComponent = DaggerAppComponent.builder()
.appModule(new AppModule((Application) getTargetContext().getApplicationContext()))
.navigationModule(new NavigationModule())
.refreshModule(new RefreshModule())
.repoModule(new RepoModule())
.sharedDataModule(new SharedDataModule(){
@Override
public SharedData providesSharedData(Application application) {
return sharedData;
}
})
.threadsModule(new ThreadsModule())
.build();
((Nerfy) getTargetContext().getApplicationContext()).setAppComponent(espressoAppComponent);
super.beforeActivityLaunched();
}
};
@Before
public void setUp() {
}
@Test
@SdkSuppress(minSdkVersion = 23)
public void a_shouldDisplayLocationPermissionRequestDialogAtStartup() throws Exception {
assertViewWithTextIsVisible(device, TEXT_ALLOW);
assertViewWithTextIsVisible(device, TEXT_DENY);
denyCurrentPermission(device);
}
没有打印任何内容。
但是如果我将模拟共享数据切换为真实数据
@Override
public SharedData providesSharedData(Application application) {
return super.providesSharedData(application);
一切运行良好,测试正确失败,因为共享数据未被模拟。 然后我还注意到应用程序的 onCreate 在测试 Activity 规则的 beforeActivityLaunched 之前被调用,尽管在一些教程中他们说它应该在创建应用程序之前调用。
我已经遵循了类似的教程 http://blog.sqisland.com/2015/04/dagger-2-espresso-2-mockito.html
Android Mocking a Dagger2 injected dependency for a Espresso test
How do you override a module/dependency in a unit test with Dagger 2.0?
和类似的;还有官方 dagger2 文档。
我尝试过对应用程序进行子类化,创建新模型和子类化组件仅用于测试;在 setUp() 结束之前也不会初始化 ActivityRule,在此处注入(inject)组件而不是重写 beforeActivityLaunched() 后。
还尝试了一些简单的修复,例如重建项目、重新启动 android studio、更新依赖项。
模拟其他一些模块也会带来相同的结果,而模拟其他模块效果很好。
有什么想法吗?
最佳答案
我测试了其他模块。发现模拟的 Refresher 模块会因具有 Observable 字段而导致测试过程崩溃。测试的 Activity 尝试订阅它,但它为空。
SharedData 更难理解——它处理共享首选项,所以基本上只是字符串和 bool 值。我必须检查所有用法。尝试将字符串值作为 url(未模拟,因此再次为 null)传递给 Picasso 以获取图像时发生崩溃。
这个故事的寓意 - 一条正确的崩溃消息值得两天的盲目调试。
关于android - 注入(inject)模拟对象时测试仪器进程崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47694783/