1

Im currently trying to write test for my method that use shared flow object and get data from it. the problem is when in trying to set method return with mockito, shared flow not emit anything and after 1 minute test result is fails.

My test:

    @ExperimentalCoroutinesApi
    @Test
    fun `when response body have error in request login`() = runBlockingTest {
        runCurrent()
        Mockito.`when`(webSocketClient.isConnect()).thenReturn(true)
        Mockito.`when`(mapper.createRPC(userLoginObject)).thenReturn(rpc)
        Mockito.`when`(requestManager.sendRequest(rpc)).thenReturn(userLoginFlow)

        userLoginFlow.emit(errorObject)
        loginServiceImpl.requestLogin(userLoginObject).drop(1).collectLatest {
            assert(it == errorObject)
        }
    } 

My method

    override fun requestLogin(userLoginObject: BaseDomain): Flow<DataState<BaseDomain>> = flow {
        emit(DataState.Loading(ProgressBarState.Loading))
        if (webSocketClient.isConnect()) {
            requestManager.sendRequest(mapper.createRPC(userLoginObject)!!)?.filterNotNull()?.collectLatest {
                if (it is IG_RPC.Error) {
                    emit(DataState.Error(ErrorObject(it.major, it.minor, it.wait)))
                } else if (it is IG_RPC.Res_User_Register) {
                    val userLoginObject = userLoginObject as UserLoginObject
                    emit(
                        DataState.Data(
                            UserLoginObject(
                                userName = it.userName,
                                phoneNumber = userLoginObject.phoneNumber,
                                userId = it.userId,
                                authorHash = it.authorHash,
                                regex = it.codeRegex,
                                resendCodeDelay = it.resendDelayTime
                            )
                        )
                    )
                }
            }
        } else {
            emit(DataState.Error(ErrorObject(-1, -1, 0)))
        }

    }

Error log:

This job has not completed yet
java.lang.IllegalStateException: This job has not completed yet

0 Answers0