Я не смог протестировать свой метод, он чувствует, что он не доходит до блока uiScope.launch, и я опубликовал свой метод viewModel, который я пытаюсь проверить, и fetchActivationCodeWithDuration
- это функция приостановки. а внизу мой тестовый класс
и я получаю это сообщение
java.lang.AssertionError:
Expected :ActivationCode(code=111111, expires=2019-05-23T10:03:50.614Z, duration=815960) Actual :null
protected val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
fun loadActivationCode() {
uiScope.launch {
progressMessageMutableData.postValue(true)
when (val result = activationCodeRepository.fetchActivationCodeWithDuration()) {
is Resource.Success<ActivationCode> -> {
progressMessageMutableData.postValue(false)
activationMutableData.postValue(result.data)
}
is Resource.Failure -> {
progressMessageMutableData.postValue(false)
errorMessageMutableData.postValue(result.message)
}
}
}
suspend fun fetchActivationCodeWithDuration(): Resource<ActivationCode> {}
Вот мой тестовый класс
@ExperimentalCoroutinesApi
@RunWith(JUnit4::class)
class ActivationViewModelTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
@UseExperimental(ObsoleteCoroutinesApi::class)
private val mainThreadSurrogate = newSingleThreadContext("UI thread")
private lateinit var viewModel: ActivationViewModel
private lateinit var serverTimeFetcher: IServerTimeFetcher
private lateinit var activationCodeRepository: ActivationRepositoryCode
@Before
fun setup() {
viewModel = ActivationViewModel()
Dispatchers.setMain(mainThreadSurrogate)
activationCodeRepository = mock(ActivationRepositoryCode::class.java)
viewModel.activationCodeRepository = activationCodeRepository
}
@After
fun tearDown() {
Dispatchers.resetMain() // reset main dispatcher to the original Main dispatcher
mainThreadSurrogate.close()
}
@Test
fun whenSuccessMenuLoad_loadActivationCode() {
runBlockingTest {
Mockito.`when`(activationCodeRepository.fetchActivationCodeWithDuration()).
thenReturn(Resource.Success(ActivationCode(code = "111111", expires = "2019-05-23T10:03:50.614Z", duration = 815960L)))
viewModel.loadActivationCode()
val expected = ActivationCode(code = "111111", expires = "2019-05-23T10:03:50.614Z", duration = 815960L)
val actual = viewModel.activationData.value
Assert.assertEquals(expected, actual)
}
}
}