我正在为我的loginviewmodel类编写单元测试用例,但是运行测试类后,出现了模仿错误。以下是我的单元测试类,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>
}