当我运行测试时,我正在使用单元测试,我得到了以下异常
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)
}
}
1条答案
按热度按时间yi0zb3m41#
@Before
是Junit4注解。如果您使用Junit5,则必须将其替换为
@BeforeEach
。