在我的应用程序中,我遵循MVP模式,在此我想为Presenter和model(interactor)类创建junit测试用例,以验证业务逻辑。
下面是我为演示者和模型编写的代码,并且我还编写了如下所述的junit测试用例。
LoginPresenterImpl.kt
class LoginPresenterImpl : LoginPresenter, LoginResponseCallback {
lateinit var loginIntegractor:LoginIntegractor
override fun loginSuccess(user: User) {
loginView.hidProgress()
loginView.loginSuucces(user)
}
override fun loginFailed(errorMessage: String) {
loginView.hidProgress()
loginView.loginFailed(errorMessage)
}
lateinit var loginView:LoginView
constructor(context: Context,loginView: LoginView){
this.loginView = loginView;
loginIntegractor = LoginInteractorImpl(context,this);
}
override fun login(userName: String, password: String) {
loginView.showProgress();
loginIntegractor.login(userName,password);
}
}
LoginInteractorImpl.kt 该文件由登录的业务逻辑组成
class LoginInteractorImpl : LoginIntegractor {
val TAG:String = LoginInteractorImpl::class.java.simpleName;
var loginResponseCallback: LoginResponseCallback;
var context: Context? = null;
constructor(context: Context,loginResponseCallback: LoginResponseCallback){
this.context = context;
this.loginResponseCallback = loginResponseCallback;
}
constructor(loginResponseCallback: LoginResponseCallback){
this.loginResponseCallback = loginResponseCallback;
}
override fun login(username: String, password: String) {
if(username.trim().isBlank()){
loginResponseCallback.loginFailed("Please enter username");
}
else if(password.trim().isBlank()){
loginResponseCallback.loginFailed("Please enter password");
}
else{
val apiService:ApiService = ApiService.Factory.create();
val jsonObject = JSONObject();
jsonObject.put("username",username);
jsonObject.put("password",password);
val call:Call<LoginResponse> = apiService.login(jsonObject.toString())
call.enqueue(object : Callback<LoginResponse> {
override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
Log.d(TAG, "login success")
if (response != null) {
val status = response.body()!!.getStatus()
if (status == 0) {
loginResponseCallback.loginSuccess(response.body()!!.getUser())
} else {
loginResponseCallback.loginFailed(response.body()!!.getMessage())
}
}
}
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
Log.d(TAG, "login failed")
loginResponseCallback.loginFailed("Something went wrong while login")
}
})
}
}
}
LoginInteractorTest.kt 。这是Juit测试用例文件。
class LoginInteractorTest {
var loginIntegractor:LoginIntegractor? = null
@Mock
private lateinit var callback: LoginResponseCallback
@Captor
private lateinit var argumentCaptor:ArgumentCaptor<LoginResponseCallback>;
@Captor
private lateinit var nameCapcture:ArgumentCaptor<String>;
@Captor
private lateinit var pwdcapcture:ArgumentCaptor<String>;
private lateinit var user: User;
@Before
fun setUp(){
callback = mock()
user = mock()
val captor = argumentCaptor<() -> Unit>()
nameCapcture = ArgumentCaptor.forClass(String::class.java)
pwdcapcture = ArgumentCaptor.forClass(String::class.java)
loginIntegractor = LoginInteractorImpl(callback)
}
@Test
fun testLogin() {
MockitoAnnotations.initMocks(this)
loginIntegractor?.login("ashok","narra")
// verify(loginIntegractor?.login(nameCapcture.capture(),pwdcapcture.capture()))
// argumentCaptor.value.loginSuccess(ArgumentMatchers.any(User::class.java))
Mockito.verify(callback).loginSuccess(ArgumentMatchers.any(User::class.java));
}
}
测试用例失败说java.lang.IllegalStateException:ArgumentMatchers.any(User :: class.java)不能为空”。有人可以建议我们如何为android中的presenter / model类实现junit测试用例。使用kotlin?
答案 0 :(得分:0)
最近我遇到了同样的问题,老实说,我无法解决,最终使用了mockito-kotlin
。
代替
Mockito.verify(callback).loginSuccess(ArgumentMatchers.any(User::class.java));
您可以简单地写:
Mockito.verify(callback).loginSuccess(any())
any()
来自嘲笑科特林。通常不需要指定类型,但是如果您需要它,可以随时像any<User>()