I can't find a solution that will help me test my ViewModel
s. I keep reading that it's enough to add rule :
@get:Rule
var rule: TestRule = InstantTaskExecutorRule()
but I keep getting :
java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details.
My test class looks like this now :
@RunWith(MockitoJUnitRunner::class)
class MainActivityViewModelTest {
@get:Rule
val taskExecutorRule = InstantTaskExecutorRule()
val weatherProviderMock = mock<WeatherProvider>()
val sut = MainActivityViewModel(weatherProviderMock, mock(), mock(), mock())
@Test
fun shouldPass() {
assertTrue(true)
}
}
I also have the following in my app build.gradle:
testImplementation 'junit:junit:4.12'
testImplementation "android.arch.core:core-testing:1.1.1"
testImplementation 'org.mockito:mockito-core:2.22.0'
testImplementation 'org.assertj:assertj-core:3.9.1'
testImplementation 'com.nhaarman:mockito-kotlin:1.6.0'
testImplementation 'org.mockito:mockito-inline:2.22.0'
Any help much appreciated.
@Chris
Can you please tell me if there's anything wrong with the code below (as per your proposed solutions):
lateinit var sut: SummaryViewModel
@get:Rule
val rule: TestRule = InstantTaskExecutorRule()
@Before
fun setUp() {
sut = SummaryViewModel(calculatorMock, mock(), mock(), mock(), providerMock, mock())
}
@Test
fun `live data test`() {
val someLiveData = sut.someLiveDataValue
assertThat(true).isTrue()
}