Я пишу тестовый блок для своего класса loginviewmodel, но после запуска тестового класса я получил ошибку mockito.Ниже приведен код моего класса модульного тестирования, класса ApiClient и класса модели представления
class LoginViewModelTest {
private var viewModel: LoginViewModel? = null
private var apiClient: ApiClient? = null
private var mLoginTestSubscriber: TestObserver<LoginResponse>? = null
private var mLoginResponse: LoginResponse? = null
companion object {
@BeforeClass
fun setupRxJavaPlugins() {
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
}
}
@Mock
private val mLoginRepository: ApiClient? = null
@Mock
private lateinit var mockContext: Context
@Before
fun init() {
MockitoAnnotations.initMocks(this)
viewModel = LoginViewModel(mockContext)
mLoginTestSubscriber = TestObserver<LoginResponse>()
mLoginResponse = TestDataGenerator.generateLoginResponse(LoginRequest("test@gmail.com", "test123"))
}
@Test
fun loginResponseWith200() {
// given the following movies
Mockito.`when`(mLoginRepository?.getLoinDetails(LoginRequest("test@gmail.com", "test123"))).thenReturn(Observable.just<LoginResponse>(mLoginResponse))
// discover popular movies
viewModel?.getLogin2("test@gmail.com", "test123")
// verify that the repository is called
Mockito.verify<ApiClient>(mLoginRepository).getLoinDetails(LoginRequest("test@gmail.com", "test123"))
Assert.assertTrue(viewModel?.mObservableResponse?.get()?.StatusCode == mLoginResponse?.StatusCode)
}
}
Здесь я получаю сообщение об ошибке -> // verify that the repository is called
Mockito.verify<ApiClient>(mLoginRepository).getLoinDetails(LoginRequest("test@gmail.com", "test123"))
следующее - подробное сообщение об ошибке после запуска класса теста:
Wanted but not invoked:
mLoginRepository.getLoinDetails(
LoginRequest(Email=test@gmail.com, Password=test123));
-> at com.loginmodule.viewmodel.LoginViewModelTest.loginResponseWith200(LoginViewModelTest.kt:100)
Actually, there were zero interactions with this mock.
Ниже приведен мой класс ViewModel, для которого я пишу пример модульного теста:
class LoginViewModel(internal var mContext: Context) : BaseObservable() {
var Email: ObservableField<String>? = ObservableField("")
var Password: ObservableField<String>? = ObservableField("")
val mObservableResponse = ObservableField<LoginResponse>()
var statusCode : Int = 2
var message : String = ""
private var mCompositeDisposable: CompositeDisposable? = CompositeDisposable()
fun onLoginClicked() {
if (CommonUtils.isEmailValid(Email?.get().toString()) && CommonUtils.isPasswordValid(Password?.get().toString()))
getLogin(Email?.get().toString(), Password?.get().toString())
else
Toast.makeText(mContext, R.string.login_error, Toast.LENGTH_SHORT).show()
}
fun getLogin(email: String, password: String) {
val apiService = ApiClient.create()
apiService.getLoinDetails(LoginRequest(email, password))
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(object : DisposableObserver<LoginResponse>() {
override fun onNext(value: LoginResponse) {
Log.e("getLogin::",value.Message);
mObservableResponse.set(value)
statusCode = value.StatusCode
message = value.Message
}
override fun onError(e: Throwable) {
}
override fun onComplete() {
}
})
}
}
interface ApiClient {
/**
* Companion object to create the ApiClient
*/
companion object Factory {
var okHttpClient = UnsafeOkHttpClient.unsafeOkHttpClient
fun create(): ApiClient {
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.baseUrl(BASE_URL)
.build()
return retrofit.create(ApiClient::class.java);
}
}
@POST("UserManagement/Login")
fun getLoinDetails(@Body credentials: LoginRequest): Observable<LoginResponse>
}