I am having some issues with a instrument test using Hilt. The problem is that I have to deal with two viewModels, one for the activity and then one for the base class. Here is what I have:
@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class TourActivityUat {
private val context: Context
get() = ApplicationProvider.getApplicationContext()
val targetContext: Context =
InstrumentationRegistry.getInstrumentation().targetContext
@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)
@get:Rule(order = 1)
val setupRepoRule = SetupRepoRule(context)
@get:Rule(order = 2)
val activityScenarioRule = ActivityScenarioRule<TourActivity>(
TourActivity.createIntent(
context
)
)
@Mock
private val userDataRepo: UserDataRepo = mock()
private val authTokenRepo: AuthTokenData = mock()
private val tokenRepo = TokenRepository(cookieJar = ClearableCookieJarTest(context))
@BindValue
val viewModel = TourViewModel(
userDataRepo = userDataRepo,
scheduler = Schedulers.computation()
)
@BindValue
val userInactivityViewModel = UserInactivityViewModel(
authTokenData = authTokenRepo,
scheduler = Schedulers.computation(),
tokenRepo = tokenRepo
)
@Before
fun setup() {
val notificationsResponse = NotificationResponse.test(
messages = 4,
documents = 0
)
whenever(userDataRepo.notifications).doReturn(
Observable.just(
notificationsResponse
))
whenever(userDataRepo.relationshipIds).doReturn(Single.just(listOf(1L)))
whenever(userDataRepo
.fetchNotifications(any())).doReturn(Completable.complete())
whenever(userDataRepo.firstName).doReturn(Single.just("Kristy"))
hiltRule.inject()
context.saveTourVisitedStatus(false)
}
//Tests
}
but I can't run any of the tests as I am getting: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
TourActivity inherits from UserInactivityActivity. Both have viewModels
Yes, you can have two viewModels in BindView. We have a class with 3.