Lateinit属性fakeAuthRepository尚未初始化单元测试

ffx8fchx  于 2022-09-21  发布在  Android
关注(0)|答案(1)|浏览(141)

当我运行测试时,我正在使用单元测试,我得到了以下异常

Ateinit属性fakeAuthRepository尚未初始化kotlin.UnInitializedPropertyAccessException:lateinit属性fakeAuthRepository尚未初始化

在我的ViewModel测试下面,测试给出一个异常

internal class SignInViewModelTest{
    private val _login =  MutableStateFlow<UiStateObject<SignInResponse>>(UiStateObject.EMPTY)

         @Mock
        lateinit var backendApi:BackendApi

    lateinit var fakeAuthRepository: FakeAuthRepository
    lateinit var authRepository: AuthRepository
    private lateinit var viewModel: SignInViewModel
    @Before
    fun setUp() {

        MockitoAnnotations.initMocks(this)
        fakeAuthRepository = FakeAuthRepository(backendApi)
        authRepository = AuthRepository(backendApi)
        viewModel = SignInViewModel(authRepository)

    }

    var login = _login

    @Test
    fun `testing repository`() = runBlockingTest {

        val fake = fakeAuthRepository.login("kyodgorbek@gmail.com", "12345678", "android", "123455")
        val real = authRepository.login("kyodgorbek@gmail.com", "12345678", "android", "123455")
        assertEquals(fake, real)

    }

}
yi0zb3m4

yi0zb3m41#

@Before是Junit4注解。

如果您使用Junit5,则必须将其替换为@BeforeEach

相关问题