Skip to content

Instantly share code, notes, and snippets.

@alexshr
Last active August 28, 2017 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexshr/c130b1f2015fc55f0df7d93a04dda9db to your computer and use it in GitHub Desktop.
Save alexshr/c130b1f2015fc55f0df7d93a04dda9db to your computer and use it in GitHub Desktop.
Extended AndroidJUnitRunner for starting with different Application class (e.g. to replace dagger component)
package com.skb.goodsapp;
import com.skb.goodsapp.di.AppComponent;
import com.skb.goodsapp.di.AppModule;
import com.skb.goodsapp.di.DaggerService;
/**
* to replace MyApplication (using MockTestRunner)
* for MockWebServer usage
*
* from manifest:
*
* <instrumentation
* android:name="com.skb.goodsapp.MockTestRunner"
* android:functionalTest="false"
* android:handleProfiling="false"
* android:label="Tests for com.skb.goodsapp"
* android:targetPackage="com.skb.goodsapp" />
*
* public class MockTestRunner extends AndroidJUnitRunner {
* @Override
* public Application newApplication(ClassLoader cl, String className, Context context)
* throws InstantiationException, IllegalAccessException, ClassNotFoundException {
* return super.newApplication(cl, MockMyApplication.class.getName(), context);
* }
*}
*/
public class MockMyApplication extends MyApplication {
private static String baseUrl;
@Override
public Object createComponent() {
return DaggerService.buildComponent(AppComponent.class, new AppModule(this) {
@Override
protected String provideBaseUrl() {
return baseUrl != null ? baseUrl : super.provideBaseUrl();
}
});
}
public static void setupBaseUrl(String baseUrl) {
MockMyApplication.baseUrl = baseUrl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment