Issue
When i executed my test code with hilt, i got above error. Which is quite werid, because everything run well in my app.
The following is my test code.
@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())
}
}
My component module
@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)
}
Any ideas for this problem?
Solution
No this is not weird because you are trying to use dagger-hilt modules
with a normal Activity that is not annotated with @AndroidEntryPoint
.
Please refer to this tutorial as it explains how to test fragments with dagger-hilt: https://www.youtube.com/watch?v=k4zG93ogWFY&t
Answered By - Andrew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.