dimanche 21 juin 2015

How can I stub method called in Activity onCreate

In my case, there's a DataHelper help me to load a data list when activity create

public interface DataHelper {
    void loadDataListAsync();
}


public class MainActivity {

    @Inject
    DataHelper mDataHelper; // inject by dagger2

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDataHelper.loadDataListAsync();
    }
    ...
}

And I can stub the mDataHelper.loadDataListAsync() method 'after getActivity' like this:

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    ...
    public void setUp() throws Exception {
        super.setUp();
        getActivity();
        tryToStubMethodLoadDataListAsync();
    }
    ...
    do some tests
    ...
}

But becuase the stub occured after the actvity calling onCreate method, there will be nothing happen to my tests.

How can I stub the methods called in the activity onCreate? Just like if I can do the stub things before calling the getActivity():

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    ...
    public void setUp() throws Exception {
        super.setUp();
        tryToStubMethodLoadDataListAsync();
        getActivity();
    }
    ...
    do some tests
    ...
}

(Btw I don't want to stub the method in the provider of dagger2 module)

Aucun commentaire:

Enregistrer un commentaire