Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Test of Retrofit 2 api call with Mockito

I need some advices on how to mock a rest api. My application is in MVP architecture.

My interface for 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 test my presenter by writing a Mock service. But I don't like that so much and I think Mockito 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("[email protected]","password");
        verify(view).loginSuccess();
    }

}

What I want to do is I mock the response data call loginSuccess() when the response is correct.

Of course my current test will not work. I need some advices on how to mock this? Any idea? Thanks.

like image 469
Bagusflyer Avatar asked May 20 '16 03:05

Bagusflyer


2 Answers

You can do it in next way:

@Test
public void testLoginWithCorrectUserNameAndPassword() throws Exception {
    // create or mock response object
    when(service.login(anyString(), anyString(), anyString).thenReturn(Observable.just(response));
    mLoginPresenter.login("[email protected]","password");
    verify(view).loginSuccess();
}

@Test
public void testLoginWithIncorrectUserNameAndPassword() throws Exception {
    // create or mock response object
    when(service.login(anyString(), anyString(), anyString).thenReturn(Observable.<Response>error(new IOException()));
    mLoginPresenter.login("[email protected]","password");
    verify(view).showError(anyString);
}
like image 189
Ilya Tretyakov Avatar answered Nov 17 '22 06:11

Ilya Tretyakov


Thanks for @Ilya Tretyakov, I came out this solution:

private ArgumentCaptor<Subscriber<Response>> subscriberArgumentCaptor;

@Test
public void testLoginWithCorrectUserNameAndPassword() throws Exception {
    mLoginPresenter.login("[email protected]","password");
    // create the mock Response object
    Response response = ......

    verify(service, times(1)).login(
        subscriberArgumentCaptor.capture(),
        stringUserNameCaptor.capture(),
        stringPasswordCaptor.capture()
    );

    subscriberArgumentCaptor.getValue().onNext(response);
    verify(view).loginSuccess();
}
like image 34
Bagusflyer Avatar answered Nov 17 '22 06:11

Bagusflyer