Last active
August 28, 2017 23:22
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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