单元测试在测试暂停功能时挂起 Android

Unit test hangs in Android while testing suspend function

我在我的应用程序的存储库层中有一个挂起函数。我正在尝试对该函数进行单元测试,但每次它执行时,它似乎都挂在 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)
    }
}

Mockito 似乎还不支持暂停功能。

因此,与其模拟 authResultTask: Task<AuthResult>(我认为这是个坏主意),不如使用 TaskCompletionSource<AuthResult> 并设置它的结果。

编辑:

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

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