Skip to content Skip to sidebar Skip to footer

Robolectric 3 With Fabric Crashlytics

I am trying to ShadowClass Crashlytics/Fabric so that Robotlectric 3 tests do not fail. What I have so far is this: The custom test runner that adds the Shadow class for Fabric: p

Solution 1:

This is my usual advice how to write a test against something not testable.

Extract you Fabric initialisation to protected method:

publicclass <MyApplicationName> {

publicvoidonCreate() {
   initFabric();
}

@VisibileForTesting
voidinitFabric() {
....
}
}

Create Test<MayApplicationName> class in test sources (same package and override Fabric initialisation:

publicclassTest<MyApplicationName> {
voidinitFabric() {
//nothing to do
}
}

Everywhere where you need using Fabric use DI (Dependency Injection) to mock Fabric in tests. Even more, I would suggest you create Analytics/Crash/Distribution class and hide Fabric usage from entire application.

And final you have left classes that wrap/hide the Fabric. Here you can write a custom shadow, spy on the real object or leave it untested. And you already tried to write custom shadow without success, also, spying is not an option here.

Happy coding!

Post a Comment for "Robolectric 3 With Fabric Crashlytics"