首页 > 解决方案 > 测试挂起功能时单元测试在Android中挂起

问题描述

我的应用程序的存储库层中有一个挂起功能。我正在尝试对该函数进行单元测试,但每次执行它似乎都会挂在await()调用上,然后永远停留在那里并且永远不会完成。看起来后台发生了一些僵局,但目前还不清楚发生了什么。

有人在这里有任何解决方案吗?

这是我的回购功能:

suspend fun login(email: String, password: String): DataState<AuthViewState> {
        try {
            val authResult = firebaseAuth
                .signInWithEmailAndPassword(email, password)
                .await()

            return DataState.data<AuthViewState>(
                data = AuthViewState(
                    authResult.user?.uid
                )
            )
        } catch (e: FirebaseAuthInvalidUserException) {
            Log.e(TAG, "loginUserIfExisting: FirebaseAuthInvalidUserException: ", e)
            return DataState.error<AuthViewState>(
                "User not found. Please register."
            )
        } catch (e: FirebaseAuthInvalidCredentialsException) {
            Log.e(TAG, "loginUserIfExisting: FirebaseAuthInvalidCredentialsException: ", e)
            return DataState.error<AuthViewState>(
                "Your password/email is incorrect. Please try again."
            )
        }
    }

这是我的模拟测试:

@RunWith(JUnit4::class)
class AuthRepositoryTest {

    private val email = "lee.a.wilson90@gmail.com"
    private val password = "fakepassword"
    private val CORRECT_USER_ID = "CORRECT_USER_ID"

    private lateinit var SUT: AuthRepository

    @Mock
    private lateinit var firebaseAuth: FirebaseAuth

    @Mock
    private lateinit var authResultTask: Task<AuthResult>

    @Mock
    private lateinit var mockResult: AuthResult

    @Mock
    private lateinit var mockFirebaseUser: FirebaseUser

    @Mock
    private lateinit var mockUserPropertiesDao: UserPropertiesDao

    @Mock
    private lateinit var mockSharedPreferences: SharedPreferences

    @Mock
    private lateinit var mockSharedPreferencesEditor: SharedPreferences.Editor

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)

        `when`(firebaseAuth.signInWithEmailAndPassword(email, password))
            .thenReturn(authResultTask)

        `when`(mockResult.user)
            .thenReturn(mockFirebaseUser)

        `when`(mockFirebaseUser.uid)
            .thenReturn("CORRECT_USER_ID")

        `when`(mockUserPropertiesDao.searchByEmail(email))
            .thenReturn(null)

        `when`(mockSharedPreferences.getString(Constants.PREVIOUS_AUTH_USER, null))
            .thenReturn(null)

        SUT = AuthRepository(
            firebaseAuth,
            mockUserPropertiesDao,
            mockSharedPreferences,
            mockSharedPreferencesEditor
        )
    }

    @Test
    fun test_correctCredentialsEntered_correctOutputState() = runBlocking {
        whenever(authResultTask.await()).thenReturn(mockResult)
        val resultState = SUT.login(email, password)
        val result = resultState.data!!.getContentIfNotHandled()!!.uid
        assertEquals(CORRECT_USER_ID, result)
    }
}

标签: androidfirebaseunit-testingmockitokotlin-coroutines

解决方案


Mockito 似乎还不支持挂起功能。

因此,不要模拟authResultTask: Task<AuthResult>(无论如何我认为这是一个坏主意),您应该使用 aTaskCompletionSource<AuthResult>并设置它的结果。

编辑:

通过解释观察到的行为来完成这个答案。

单元测试挂起,因为在您模拟函数Continuation<T>.resume()时没有被 mockito调用。await()


推荐阅读