I have a View Model that extends AndroidViewModel
class MoveViewModel(application: Application): AndroidViewModel(application),CoroutineScope{
....
}
And I want to unit test it but I cannot figure out how to Mock the Application class
@Test
fun testSearchDataValidation() {
val application = Mockito.mock(Application::class.java)
val viewModel = MoveViewModel(application)
.....
}
But when I go to run the test I get an error that Mockito cannot mock Application
org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class android.app.Application.
Mockito can only mock non-private & non-final classes.
How do I mock the Application class to pass it to my view model?
Edit:
Here is my folder hierarchy as suggested by @farhanjk


testfolder? - tyczjtestfolder, you would need to mock all Android dependencies (likeApplicationorSharedPreferences), tests inandroidTestfolder instead have your actual application available. You should be able to unit test view models intest, they're much faster thanandroidTest. - lelloman