我有一个带有某些依赖项的ContentProvider
,使用Dagger
注入它们。
@Override
public boolean onCreate()
{
AndroidInjection.inject(this);
...
...
}
如果我想通过以下规则在工具测试中测试ContentProvider
:
@Rule
public ProviderTestRule mProviderRule =
new ProviderTestRule.Builder(MyContentProvider.class,TEST_AUTHORITY)
.build();
如果我运行测试,则在ClassCastException
通话中会收到inject()
。
java.lang.ClassCastException:androidx.test.rule.provider.DelegatingContext无法转换为android.app.Application 在dagger.android.AndroidInjection.inject(AndroidInjection.java:164) 它在测试运行期间接缝了一个名为DelegatingContext而不是Application的自定义上下文对象。那么有什么解决方案可以编写这样的测试吗?