error: [Dagger/MissingBinding] @com.zhixin.wedeep.common.di.LoginApi okhttp3.OkHttpClient cannot be provided without an @Provides-annotated method
error: [Dagger/MissingBinding] @com.zhixin.wedeep.common.di.LoginApi okhttp3.OkHttpClient cannot be provided without an @Provides-annotated method
当我用刀柄执行我的测试代码时,出现了上述错误。这很奇怪,因为我的应用程序中的一切 运行 都很好。
以下是我的测试代码
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
@LargeTest
class TestHomePageFragment {
private val intent = Intent(ApplicationProvider.getApplicationContext(), AudioPlayerActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
putExtra(HomePageArouterConstants.KEY_ID,"1")
}
@get:Rule
val activityRule:ActivityScenarioRule<AudioPlayerActivity> = activityScenarioRule(intent)
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Before
fun init(){
hiltRule.inject()
}
@Test
fun testVote(){
// Assert.assertEquals(1,1)
val scenario = activityRule.scenario
onView(withId(R.id.linear_layout_like)).perform(click())
}
}
我的组件模块
@Module
@InstallIn(ApplicationComponent::class)
object NetWorkModule {
@Provides
@Singleton
fun provideOkHttpClient(interceptor: HttpLoggingInterceptor): OkHttpClient =
OkHttpClient.Builder()
.addInterceptor(HeaderInterceptor())
.addInterceptor(interceptor)
.addNetworkInterceptor(StethoInterceptor())
.build()
@Provides
@Singleton
fun provideLoggingInterceptor() =
HttpLoggingInterceptor().apply { level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE }
@Provides
@Singleton
fun provideGson(): Gson = Gson()
@Provides
@Singleton
fun provideGsonConverterFactory(gson: Gson): GsonConverterFactory =
GsonConverterFactory.create(gson)
@CoroutineScropeIO
@Provides
fun provideCoroutineScopeIO() = CoroutineScope(Dispatchers.IO)
}
对这个问题有什么想法吗?
不,这并不奇怪,因为您正在尝试将 dagger-hilt modules
与未使用 @AndroidEntryPoint
.
注释的普通 Activity 一起使用
请参阅本教程,因为它解释了如何使用 dagger-hilt 测试片段:https://www.youtube.com/watch?v=k4zG93ogWFY&t
当我用刀柄执行我的测试代码时,出现了上述错误。这很奇怪,因为我的应用程序中的一切 运行 都很好。
以下是我的测试代码
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
@LargeTest
class TestHomePageFragment {
private val intent = Intent(ApplicationProvider.getApplicationContext(), AudioPlayerActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
putExtra(HomePageArouterConstants.KEY_ID,"1")
}
@get:Rule
val activityRule:ActivityScenarioRule<AudioPlayerActivity> = activityScenarioRule(intent)
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Before
fun init(){
hiltRule.inject()
}
@Test
fun testVote(){
// Assert.assertEquals(1,1)
val scenario = activityRule.scenario
onView(withId(R.id.linear_layout_like)).perform(click())
}
}
我的组件模块
@Module
@InstallIn(ApplicationComponent::class)
object NetWorkModule {
@Provides
@Singleton
fun provideOkHttpClient(interceptor: HttpLoggingInterceptor): OkHttpClient =
OkHttpClient.Builder()
.addInterceptor(HeaderInterceptor())
.addInterceptor(interceptor)
.addNetworkInterceptor(StethoInterceptor())
.build()
@Provides
@Singleton
fun provideLoggingInterceptor() =
HttpLoggingInterceptor().apply { level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE }
@Provides
@Singleton
fun provideGson(): Gson = Gson()
@Provides
@Singleton
fun provideGsonConverterFactory(gson: Gson): GsonConverterFactory =
GsonConverterFactory.create(gson)
@CoroutineScropeIO
@Provides
fun provideCoroutineScopeIO() = CoroutineScope(Dispatchers.IO)
}
对这个问题有什么想法吗?
不,这并不奇怪,因为您正在尝试将 dagger-hilt modules
与未使用 @AndroidEntryPoint
.
请参阅本教程,因为它解释了如何使用 dagger-hilt 测试片段:https://www.youtube.com/watch?v=k4zG93ogWFY&t