"Check that you have added the HiltAndroidRule" 用于测试中的 ContentProvider
"Check that you have added the HiltAndroidRule" for ContentProvider in a test
我有一个使用内容提供程序启动应用程序的插桩测试。
测试很简单:
@HiltAndroidTest
@UninstallModules({...})
public class MyTest {
@Rule
public HiltAndroidRule hiltRule = new HiltAndroidRule(this);
@Test
...
}
但是,被测应用程序包含一个 ContentProvider
,并且此提供程序正在使用 Hilt 注入数据:
public class MyProvider extends ContentProvider {
@EntryPoint
@InstallIn(ApplicationComponent.class)
interface MyProviderEntryPoint {
@SqliteDatabaseName String databaseName();
}
@Override
synchronized public boolean onCreate() {
Context appContext = getContext().getApplicationContext();
MyProviderEntryPoint entryPoint =
EntryPointAccessors.fromApplication(appContext, MyProviderEntryPoint.class);
mOpenHelper = new SqliteStoreOpener(getContext(), entryPoint.databaseName());
return true;
}
这会导致测试在启动时崩溃:
java.lang.RuntimeException: Unable to get provider com.test.MyProvider: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
at android.app.ActivityThread.installProvider(ActivityThread.java:6242)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
at dagger.hilt.internal.Preconditions.checkState(Preconditions.java:83)
at dagger.hilt.android.internal.testing.TestApplicationComponentManager.generatedComponent(TestApplicationComponentManager.java:79)
at dagger.hilt.android.testing.HiltTestApplication.generatedComponent(HiltTestApplication.java:49)
at dagger.hilt.EntryPoints.get(EntryPoints.java:46)
at dagger.hilt.android.EntryPointAccessors.fromApplication(EntryPointAccessors.java:36)
at com.test.MyProvider.onCreate(EboBirthdayProvider.java:114)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1917)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1892)
at android.app.ActivityThread.installProvider(ActivityThread.java:6239)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我想问题是框架试图在 Hilt 有机会启动和创建组件之前安装提供程序。允许 Hilt 先 运行 然后构建提供程序的正确设置是什么?作为参考,我有一个简单的测试 运行ner:
public class MyRunner extends AndroidJUnitRunner {
@Override
public Application newApplication(ClassLoader cl, String className, Context context)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(cl, HiltTestApplication.class.getName(), context);
}
这是非常反高潮的,但它最终成为 build.gradle
文件中的一个问题。
注释处理器已添加为
annotationProcessor 'com.google.dagger:hilt-android-compiler:2.30.1-alpha'
而不是
androidTestAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.30.1-alpha'
我有一个使用内容提供程序启动应用程序的插桩测试。
测试很简单:
@HiltAndroidTest
@UninstallModules({...})
public class MyTest {
@Rule
public HiltAndroidRule hiltRule = new HiltAndroidRule(this);
@Test
...
}
但是,被测应用程序包含一个 ContentProvider
,并且此提供程序正在使用 Hilt 注入数据:
public class MyProvider extends ContentProvider {
@EntryPoint
@InstallIn(ApplicationComponent.class)
interface MyProviderEntryPoint {
@SqliteDatabaseName String databaseName();
}
@Override
synchronized public boolean onCreate() {
Context appContext = getContext().getApplicationContext();
MyProviderEntryPoint entryPoint =
EntryPointAccessors.fromApplication(appContext, MyProviderEntryPoint.class);
mOpenHelper = new SqliteStoreOpener(getContext(), entryPoint.databaseName());
return true;
}
这会导致测试在启动时崩溃:
java.lang.RuntimeException: Unable to get provider com.test.MyProvider: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
at android.app.ActivityThread.installProvider(ActivityThread.java:6242)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
at dagger.hilt.internal.Preconditions.checkState(Preconditions.java:83)
at dagger.hilt.android.internal.testing.TestApplicationComponentManager.generatedComponent(TestApplicationComponentManager.java:79)
at dagger.hilt.android.testing.HiltTestApplication.generatedComponent(HiltTestApplication.java:49)
at dagger.hilt.EntryPoints.get(EntryPoints.java:46)
at dagger.hilt.android.EntryPointAccessors.fromApplication(EntryPointAccessors.java:36)
at com.test.MyProvider.onCreate(EboBirthdayProvider.java:114)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1917)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1892)
at android.app.ActivityThread.installProvider(ActivityThread.java:6239)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我想问题是框架试图在 Hilt 有机会启动和创建组件之前安装提供程序。允许 Hilt 先 运行 然后构建提供程序的正确设置是什么?作为参考,我有一个简单的测试 运行ner:
public class MyRunner extends AndroidJUnitRunner {
@Override
public Application newApplication(ClassLoader cl, String className, Context context)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(cl, HiltTestApplication.class.getName(), context);
}
这是非常反高潮的,但它最终成为 build.gradle
文件中的一个问题。
注释处理器已添加为
annotationProcessor 'com.google.dagger:hilt-android-compiler:2.30.1-alpha'
而不是
androidTestAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.30.1-alpha'