dimanche 22 novembre 2015

Determine for which test setup() is being called?

I'm writing Unit tests for my Android App using Mockito. I'm using dagger 2 to switch between a Mock Client and a real client:

@Module
public class ApiModule {

    private boolean mMockMode;

    @Provides
    @Singleton
    Client provideClient() {

        if (mMockMode) {
                return Mockito.mock(Client.class);
        }

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.networkInterceptors().add(new StethoInterceptor());

        return new OkClient(okHttpClient);

    }

}

The client is injected into the tests like this:

public class MainActivityTest extends ActivityInstrumentationTestCase2<BaseActivity> {  

    @Inject
    Client client;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        App app = (App) getInstrumentation().getTargetContext().getApplicationContext();
        app.setMockMode(true);
        app.component().inject(this);
    }

    @Test
    public void testWithActualCall(){
        //code...
    }

    @Test
    public void testWithMockCall(){
        //code...
    }

    @Override
    protected void tearDown() throws Exception {
        App.getInstance().setMockMode(false);
    }
}

As can be seen from the code, there are some tests that I wish to do with a mock api and others that I wish to do with the real api. In order to do this, I need to know for which test setUp() is being called so that I can change the argument of setMockMode(boolean) accordingly.

Is there a way I can find out which test is being setUp()?

Aucun commentaire:

Enregistrer un commentaire