【备忘踩坑】Android单元测试中读取测试assets的方法
1、build.gradle中添加assets
设置完成后要运行File->Sync project with gradle files
sourceSets {...androidTest {assets.srcDirs = ['src/androidTest/assets']}}
2、读取时注意context
一定是InstrumentationRegistry.getInstrumentation().getContext(),网上有很多种说法,有的用了Deprecated的类,也有用InstrumentationRegistry.getInstrumentation().getTargetContext(),如果用getTargetContext则获取的是主应用的context,读取的assets也是主应用的,而不是androidTest/assets中的
Context context = InstrumentationRegistry.getInstrumentation().getContext();
AssetsWrapper asset = new AssetsWrapper(context.getAssets());