Created
June 29, 2017 13:05
-
-
Save OleksandrKucherenko/a9e9360c7e1e27ece796a25d1bdc5587 to your computer and use it in GitHub Desktop.
Workaround for Robolectric Unit Tests with Android Dev Metrics tool attached
This file contains hidden or 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
public class TheApp extends Application { | |
@Override | |
public void onCreate() { | |
// now give the app code do the rest: launch activities, etc. | |
super.onCreate(); | |
// Use it only in debug builds | |
if (BuildConfig.DEBUG) { | |
initializeAndroidDevMetrics(this); | |
} | |
} | |
/** Is detected robolectric test environment. */ | |
public static boolean isRobolectric() { | |
return "robolectric".equals(Build.FINGERPRINT); | |
} | |
/** Initialize dev metrics, but disable them for Robolectric tests. */ | |
public static void initializeAndroidDevMetrics(@NonNull final Context context) { | |
// https://stackoverflow.com/questions/36479391/roboelectric-unit-tests-are-unresponsive | |
final boolean enabled = !isRobolectric(); | |
final AndroidDevMetrics.Builder builder = new AndroidDevMetrics.Builder(context) | |
.enableActivityMetrics(enabled) | |
.enableDagger2Metrics(enabled) | |
.showNotification(enabled); | |
AndroidDevMetrics.initWith(builder); | |
// important line! resolve issue of initialization | |
MethodsTracingManager.getInstance().init(context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment