I'm using almost the as same architecture as Google sample: GithubBrowserSample.
This field is injected in my Fragment class:
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
In all my fragments tests I got this error which corresponds to a field that is instantiated by injection:
java.lang.RuntimeException: kotlin.UninitializedPropertyAccessException: lateinit property viewModelFactory has not been initialized
This happens when I set my fragment like this with FragmentScenario:
viewModel = mock(TrainingListViewModel::class.java)
val scenario = launchFragmentInContainer(themeResId = R.style.Theme_LafayWorkbook) {
TrainingListFragment().apply {
appExecutors = countingAppExecutors.appExecutors
viewModelFactory = ViewModelUtil.createFor(viewModel)
}
}
dataBindingIdlingResourceRule.monitorFragment(scenario)
scenario.onFragment { fragment ->
Navigation.setViewNavController(fragment.requireView(), navController)
fragment.binding.trainingListRv.itemAnimator = null
fragment.disableProgressBarAnimations()
}
But when I setup the Fragment before using it with FragmentScenario it works:
viewModel = mock(TrainingListViewModel::class.java)
val trainingListFragment = TrainingListFragment()
trainingListFragment.viewModelFactory = ViewModelUtil.createFor(viewModel)
trainingListFragment.appExecutors = countingAppExecutors.appExecutors
trainingListFragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
if (viewLifecycleOwner != null) {
Navigation.setViewNavController(trainingListFragment.requireView(), navController)
}
}
val scenario = launchFragmentInContainer(themeResId = R.style.Theme_LafayWorkbook) {
trainingListFragment
}
Here is the source code of my test:
@RunWith(AndroidJUnit4::class)
class TrainingListFragmentTest {
@Rule
@JvmField
val executorRule = TaskExecutorWithIdlingResourceRule()
@Rule
@JvmField
val countingAppExecutors = CountingAppExecutorsRule()
@Rule
@JvmField
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule()
private val navController = mock<NavController>()
private val trainingsLiveData = MutableLiveData<Resource<List<Training>>>()
private val isPremiumLiveData = MutableLiveData<PremiumAccount>()
private val trainingDeletedLiveData = MutableLiveData<Event<Boolean>>()
private val navigateToCreateTrainingLiveData = MutableLiveData<Event<Boolean>>()
private val navigateToExerciseListLiveData = MutableLiveData<Event<Training>>()
private lateinit var viewModel: TrainingListViewModel
@Before
fun init() {
viewModel = mock(TrainingListViewModel::class.java)
val trainingListFragment = TrainingListFragment()
trainingListFragment.viewModelFactory = ViewModelUtil.createFor(viewModel)
trainingListFragment.appExecutors = countingAppExecutors.appExecutors
trainingListFragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
if (viewLifecycleOwner != null) {
Navigation.setViewNavController(trainingListFragment.requireView(), navController)
}
}
`when`(viewModel.trainingsLiveData).thenReturn(trainingsLiveData)
`when`(viewModel.isPremium).thenReturn(isPremiumLiveData)
`when`(viewModel.trainingDeleted).thenReturn(trainingDeletedLiveData)
`when`(viewModel.navigateToCreateTraining).thenReturn(navigateToCreateTrainingLiveData)
`when`(viewModel.navigateToExerciseList).thenReturn(navigateToExerciseListLiveData)
val scenario = launchFragmentInContainer(themeResId = R.style.Theme_LafayWorkbook) {
trainingListFragment
}
dataBindingIdlingResourceRule.monitorFragment(scenario)
scenario.onFragment { fragment ->
Navigation.setViewNavController(fragment.requireView(), navController)
fragment.binding.trainingListRv.itemAnimator = null
fragment.disableProgressBarAnimations()
}
}
}
Do you guys have an idea about why this is happening and how I can fix it? I'm really struggling on this :(
Aucun commentaire:
Enregistrer un commentaire