使用Dagger2进行浓缩咖啡测试
问题描述:
我想在我的Espresso测试中注入由匕首创建的组件。使用Dagger2进行浓缩咖啡测试
组件应该是相同的,所以,这是没有必要重写任何东西从匕首。
我有下面的类:
@Singleton
@Component(modules = arrayOf(AppModule::class, DatabaseModule::class, RepositoryModule::class))
interface TestComponent: AppComponent {
fun inject(accountRepositoryTest: AccountRepositoryTest)
}
但绝不匕首:
@RunWith(AndroidJUnit4.class)
public class AccountRepositoryTest {
@Inject
AccountRepository repository;
@Before
public void setUp() throws Exception {
new DaggerTestComponent().builder().build().inject(this);
}
}
既然不能添加AccountRepositoryTest我的主要DaggerComponent类,我在androidTests文件夹中创建另一个组件类从TestComponent接口生成ComponentClass,当我编译代码时,我总是收到此错误:
Error:(26, 7) error: cannot find symbol class DaggerTestComponent
如果我评论这一行,我的代码会编译,所以我确信这正是阻止匕首生成类的原因。
所以我的问题是:如何使匕首从androidTests文件夹中定义的接口生成组件类?
答
解决方案是将匕首编译器添加到androidTest依赖项。
如果您正在使用科特林:
kaptAndroidTest "com.google.dagger:dagger-compiler:$daggerVersion"
如果您使用的是Java:
androidTestAnnotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"