我正在根据mvp模式为app编写mockito测试。以下是登录活动的代码
public class ClassThatNeedsABetterName<T> : List<T>
where T : A, I
{ }
我在编写检查时遇到问题public class LoginActivity extends AppCompatActivity implements LoginContract.ILoginView{
presenter = new LoginPresenter(this);
@Override
public void startMoviesActivity() {
startActivity(new Intent(this, MoviesActivity.class));
}
}
public class LoginContract {
public interface ILoginView {
...
void startMoviesActivity();
}
public interface ILoginService {
interface OnLoginFinishedListener {
void onLoginSuccess();
void onLoginFailure();
}
void login(String username, String password, OnLoginFinishedListener
listener);
}
}
public class LoginPresenter implements LoginContract.ILoginService.OnLoginFinishedListener {
private LoginContract.ILoginView view;
private LoginContract.ILoginService service;
public LoginPresenter(LoginContract.ILoginView view) {
this.view = view;
service = new LoginService();
}
public void validateCredentials(String email, String password) {
if(email.isEmpty()){
view.setEmailEmptyError(R.string.error_email_empty);
return;
}
if(password.isEmpty()){
view.setPasswordEmptyError(R.string.error_password_empty);
return;
}
view.showProgressDialog();
service.login(email,password,this);
}
@Override
public void onLoginSuccess() {
view.hideProgressDialog();
view.startMoviesActivity();
}
@Override
public void onLoginFailure() {
view.hideProgressDialog();
view.showUnsuccessfulLoginError(R.string.error_login_unsuccessful);
}
}
public class LoginService implements LoginContract.ILoginService {
@Override
public void login(String username, String password, final
OnLoginFinishedListener listener) {
// Contains REST API which calls listener.onLoginSuccess() on valid credentials and listener.onLoginFailure on invalid.
}
}
&amp;由于我使用接口的方式,onLoginSuccess()
类中的onLoginFailure()
。当致电LoginPresenter
时,它应该返回service.login(email,password,this);
中对这些方法的调用,而LoginPresenter
中的startMoviesActivity()
方法会调用LoginActivity
方法。我是模拟的初学者......如果你能提供帮助的话。谢谢。
答案 0 :(得分:0)
在下面找到一些使用mockito为您的代码编写的测试,这些我在我的机器上测试过的情况。但是,为了测试android,我更喜欢使用Robolectric(http://robolectric.org/),它基本上是为Android UI单元测试设计的,并且它是良好的w.r.t代码覆盖观点。与mockito相比,覆盖率更高。
import org.junit.Before;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
@PrepareForTest(LoginActivity.class)
public class LoginActivityTest {
private LoginActivity loginActivity;
@Before
public void setUp()
{
loginActivity = mock(LoginActivity.class);
doCallRealMethod().when(loginActivity).startMoviesActivity();
}
@Test
public void testStartMovies()
{
loginActivity.startMoviesActivity();
}
}
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@PrepareForTest(LoginPresenter.class)
public class LoginPresenterTest {
private LoginPresenter loginPresenter;
@Before
public void setUp() {
loginPresenter = mock(LoginPresenter.class);
doCallRealMethod().when(loginPresenter).onLoginSuccess();
doCallRealMethod().when(loginPresenter).onLoginFailure();
}
@Test
public void testLoginSuccess()
{
loginPresenter.onLoginSuccess();
loginPresenter.onLoginSuccess();
}
@Test
public void testLoginFailure()
{
loginPresenter.onLoginFailure();
}
}