Instrumental testing of a look at MVP

I am developing an Android application with MVP architecture, I was able to test the classes Presenter and Model, but now I am trying to test the View methods. For example, I have the following view:

public interface SplashView extends BaseMVPView {

    void initPresenter();

    void navigateToHome();

    void onError(ApiError apiError);
}

which is implemented by Activity.

public class SplashActivity extends BaseActivity implements SplashView {

    // MVP Presenter
    private SplashPresenter splashPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void destroy() {
        if(splashPresenter != null) {
            splashPresenter.onDestroy();
            splashPresenter = null;
        }
    }

    @Override
    public void initPresenter() {
        if(splashPresenter == null) {
            splashPresenter = new SplashPresenterImpl(this, ApiClient.getService());
            sync();
        }
    }

    @Override
    public void navigateToHome() {
        NavigationUtils.navigateToActivity(this, MainActivity.class, true);
    }

    @Override
    public void onError(ApiError apiError) {
        DialogUtils.showOKCancelDialog(...);
    }

    private void sync() {
        if(splashPresenter != null) {
            splashPresenter.sync();
        }
    }
}

As you can see, when an action is created, it initializes the presenter and calls a method that will receive some data from the API. Once the API call is complete, the host will call either the navigateToHome method or the onError method. Therefore, I would like to test this process for both cases. I suppose this should be an instrumental test, but I don’t know how to deal with these cases and how to call methods.

Thank you so much

+4
2

, MVP , Mocking. , , Github, :

http://github.com/mmirhoseini/fyber_mobile_offers

, Mock SplashPresenter , , , .

initPresenter:

class MockSplashActivity extends SplashActivity {
    @Override
    public void initPresenter() {
        if (splashPresenter == null) {
            splashPresenter = new MockSplashPresenterImpl(this, ApiClient.getService());
            sync();
        }
    }
}

, , , ( , - ):

class MockSplashPresenterImpl extends SplashPresenterImpl {
    public MockSplashPresenterImpl(SplashActivity splashActivity, Object service) {
        super(splashActivity, service);
    }

    @Override
    public void sync() {
        splashActivity.doSomethingYouWant();
    }
}

, :)

+1

Source: https://habr.com/ru/post/1657348/


All Articles