I need tips on how to taunt the rest api. My application is in MVP architecture.
My interface for the API:
public interface MyAPI { @GET("{cmd}/{userName}/{password}") Observable<Response> login( @Path("cmd") String cmd, @Path("userName") String userName, @Path("password") String password );
My service:
public class MyService implements IService { private static MyService mInstance = new MyService(); private MyAPI mApi; public static MyService getInstance() { return mInstance; } private MyService() { OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); httpClientBuilder.connectTimeout(Config.DEFAULT_TIMEOUT, TimeUnit.SECONDS); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Config.kBaseUrl) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(httpClientBuilder.build()) .build(); this.mApi = retrofit.create(MyAPI.class); } public void login( Subscriber<Response> subscriber, String userName, String password) { mApi.login(Config.kLoginCmd,userName,password) .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(subscriber); }
My presenter class:
public class LoginPresenter implements LoginContract.Presenter { LoginContract.View mView; IService mService; ISession mSession; public LoginPresenter(LoginContract.View loginView, IService service, ISession session) { mView = loginView; mService = service; mSession = session; } @Override public void login(String email, String password) { Subscriber<Response> subscriber = new Subscriber<Response>() { @Override public void onCompleted() { mView.showLoading(false); } @Override public void onError(Throwable e) { mView.showError(e.getLocalizedMessage()); } @Override public void onNext(Response response) { if (response.getResults().getStatus().equalsIgnoreCase(Config.kResultCodeOK)) { mView.loginSuccess(); } else { mView.showError(response.getResults().getStatus().getErrmsg()); } } }; mView.showLoading(true); mService.login( subscriber, email, password); }
There is another way to check my moderator by writing a Mock service. But I donβt like it very much, and I think that Mokito could help.
Here is my test class:
public class LoginPresenterMockTest { private LoginPresenter mLoginPresenter; @Mock LoginContract.View view; @Mock IService service; @Mock ISession session; @Before public void setup() throws Exception { MockitoAnnotations.initMocks(this); mLoginPresenter = new LoginPresenter(view, service, session); } @Test public void testLoginWithCorrectUserNameAndPassword() throws Exception { mLoginPresenter.login(" user@email.com ","password"); verify(view).loginSuccess(); } }
What I want to do is that I make fun of the loginSuccess () response data request when the answer is correct.
Of course, my current test will not work. I need tips on how to taunt this? Any ideas? Thanks.
android unit-testing mockito retrofit2
Zhou hao
source share