请检查此代码。当我尝试使用@BindValue添加存储库时。刀柄正在抛出错误,如下
请检查此代码。当我尝试使用@BindValue添加存储库时。刀柄抛出错误如下。
@HiltAndroidTest
@UninstallModules(AppModule::class)
@RunWith(AndroidJUnit4::class)
class RandomUserListFragmentTest {
@get:Rule
val hiltRule = HiltAndroidRule(this)
@Inject
lateinit var glideRequestManager: RequestManager
@BindValue
@JvmField val repository:RandomUserRepository= RandomUserRepositoryImplTest()
@Before
fun launchFragment() {
hiltRule.inject()
ActivityScenario.launch(RandomUserActivity::class.java)
}
@Test
fun verifyTheRecyclerViewHasData(){
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).check { view, noViewFoundException ->
if (noViewFoundException != null) {
throw noViewFoundException
}
val recyclerView = view as RecyclerView
Assert.assertEquals(3, recyclerView.adapter?.itemCount)
}
}
}
@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [AppModule::class]
)
object FakeAppModuleTest {
@Provides
@Singleton
fun initGlide(@ApplicationContext appContext: Context): RequestManager = Glide.with(appContext)
.setDefaultRequestOptions(
RequestOptions()
.centerInside()
.error(R.drawable.ic_user)
)
}
得到类似的错误
任务:应用:Hiltjavacompiledebugandroidtest /Users/shaheer/AndroidStudioProjects/ssassignment/app/build/generated/hilt/component_sources/debugAndroidTest/com/ss/assignment/ui/random_user_ui/RandomUserActivityTest_TestComponentDataSupplier.java:8: error: cannot find symbol 导入匕首。hilt.android.internal.testing.root.daggerRandomuseractivityTest_hiltComponents_singletonc; ^ 符号:daggerrandomuseractivityTest_hiltComponents_singletonc 位置:软件包匕首。hilt.android.internal.testing.root /users/shaheer/androidStudioprojects/ssassignment/app/build/generated/hilt/hilt/component_sources/debugandroidtest/debugandroidtest/sss/assignment/signment/comignment/comignment/ui/random_user_user_user_user_us_ui/main_screen/main_screen/randomusprier: 导入匕首。hilt.android.internal.testing.root.daggerrandomuserlistfragmenttest_hiltcomponents_singletonc; ^ 符号:DaggerrandomuserlistFragmentTest类dagrandomuserlisttest_hiltcomponents_singletonc 位置:软件包匕首。hilt.android.internal.testing.root /users/shaheer/androidStudioprojects/ssassignment/app/build/generated/hilt/-component_sources/debugandroidtest/debugandroidtest/hilt/drogger/hilt/android/android/internal/ternals/terners/terners/terners/randomuseractivelive/randomuseractivetivity一下如果没有 @uffice-nantotated方法,则无法提供sissionment.data.repository.randomuserrepository。 公共摘要静态类Singletonc实现SunandSandsapp_generatedInjector, ^ com.ss.signment.data.repository.randomuserrepository注入 com.ss.signment.ui.random_user_ui.randomusersharedViewModel(RandomuserRepository) com.ss.signment.ui.random_user_ui.randomusersharedviewmodel注入 com.ss.signment.ui.random_user_ui.randomusersharedviewmodel_hiltmodules.bindsmodule.binds(arg0) @dagger.hilt.android.internal.lifecycle.hiltviewmodelmap java.util.map< java.lang.string,javax.inject.inject.provider< androidx.lifecycle.viewmodcle.viewmodel>请求 匕首。 ivitestest_hiltComponents.ActivityRetainedC→ Dagger.hilt.android.internal.testing.root.randomuseractivitytest_hiltcomponents.viewmodelc] 3个错误
如果我在这里犯了一些错误,请纠正我...
please check this code. as I am trying to add the repository with @BindValue. the hilt is throwing error as follows.
@HiltAndroidTest
@UninstallModules(AppModule::class)
@RunWith(AndroidJUnit4::class)
class RandomUserListFragmentTest {
@get:Rule
val hiltRule = HiltAndroidRule(this)
@Inject
lateinit var glideRequestManager: RequestManager
@BindValue
@JvmField val repository:RandomUserRepository= RandomUserRepositoryImplTest()
@Before
fun launchFragment() {
hiltRule.inject()
ActivityScenario.launch(RandomUserActivity::class.java)
}
@Test
fun verifyTheRecyclerViewHasData(){
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).check { view, noViewFoundException ->
if (noViewFoundException != null) {
throw noViewFoundException
}
val recyclerView = view as RecyclerView
Assert.assertEquals(3, recyclerView.adapter?.itemCount)
}
}
}
@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [AppModule::class]
)
object FakeAppModuleTest {
@Provides
@Singleton
fun initGlide(@ApplicationContext appContext: Context): RequestManager = Glide.with(appContext)
.setDefaultRequestOptions(
RequestOptions()
.centerInside()
.error(R.drawable.ic_user)
)
}
getting errors like
Task :app:hiltJavaCompileDebugAndroidTest
/Users/shaheer/AndroidStudioProjects/ssassignment/app/build/generated/hilt/component_sources/debugAndroidTest/com/ss/assignment/ui/random_user_ui/RandomUserActivityTest_TestComponentDataSupplier.java:8: error: cannot find symbol
import dagger.hilt.android.internal.testing.root.DaggerRandomUserActivityTest_HiltComponents_SingletonC;
^
symbol: class DaggerRandomUserActivityTest_HiltComponents_SingletonC
location: package dagger.hilt.android.internal.testing.root
/Users/shaheer/AndroidStudioProjects/ssassignment/app/build/generated/hilt/component_sources/debugAndroidTest/com/ss/assignment/ui/random_user_ui/main_screen/RandomUserListFragmentTest_TestComponentDataSupplier.java:8: error: cannot find symbol
import dagger.hilt.android.internal.testing.root.DaggerRandomUserListFragmentTest_HiltComponents_SingletonC;
^
symbol: class DaggerRandomUserListFragmentTest_HiltComponents_SingletonC
location: package dagger.hilt.android.internal.testing.root
/Users/shaheer/AndroidStudioProjects/ssassignment/app/build/generated/hilt/component_sources/debugAndroidTest/dagger/hilt/android/internal/testing/root/RandomUserActivityTest_HiltComponents.java:133: error: [Dagger/MissingBinding] com.ss.assignment.data.repository.RandomUserRepository cannot be provided without an @Provides-annotated method.
public abstract static class SingletonC implements SunAndSandsApp_GeneratedInjector,
^
com.ss.assignment.data.repository.RandomUserRepository is injected at
com.ss.assignment.ui.random_user_ui.RandomUserSharedViewModel(randomUserRepository)
com.ss.assignment.ui.random_user_ui.RandomUserSharedViewModel is injected at
com.ss.assignment.ui.random_user_ui.RandomUserSharedViewModel_HiltModules.BindsModule.binds(arg0)
@dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [dagger.hilt.android.internal.testing.root.RandomUserActivityTest_HiltComponents.SingletonC → dagger.hilt.android.internal.testing.root.RandomUserActivityTest_HiltComponents.ActivityRetainedC → dagger.hilt.android.internal.testing.root.RandomUserActivityTest_HiltComponents.ViewModelC]
3 errors
please correct me if I am doing some mistakes here...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我遇到了相同的错误,但是问题实际上是我
appmoduletest
中缺少@provides
方法。我已经在appModule
类中添加了一种新方法,但忘了将其包含在我的测试模块中。即使我的任何测试都没有使用新值,但将其添加到测试模块中解决了问题并允许我的测试运行。I had the same error, but the issue was actually a missing
@Provides
method in myAppModuleTest
. I had added a new method to myAppModule
class but forgot to include it in my test module. Even though the new value wasn't being used by any of my tests, adding it to the test module solved the problem and allowed my tests to run.