I am having trouble creating a test class. Basically I want to test my view model class which performs network call. The class network component is injected by using dagger needs the context parameter to check connection, this is my view model:
class MyViewModel(application: Application): AndroidViewModel(application) {
@Inject lateinit var network: NetworkService
init {
DaggerNetworkComponent.builder().networkModule(NetworkModule(application.applicationContext))
.build().inject(this)
network.callNetwork()
}
}
And the test class is something like
lateinit var myViewModel: MyViewModel
@Test
fun testMyNetwork() {
application = Mockito.mock(Application::class.java)
myViewModel = MyViewModel(application)
}
The application.applicationContext always returns null which then returns IllegalStateException
Is there any solution for this?