I'm trying to mock an instance of Firebase Analytics for Android unit testing, but my Firebase variable remains undefined after I call Firebase's getInstance()
method - which should initialize an instance of the FirebaseAnalytics class.
I'm using PowerMock and Android Studio. Here's my Test.java file (no tests yet - just trying to initialize a mock of the class):
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 18, manifest =
Config.NONE)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@PrepareForTest({FirebaseAnalytics.class})
public class FirebaseTest {
@Rule public PowerMockRule rule = new PowerMockRule();
@Mock Application application;
@Mock Analytics analytics;
FirebaseAnalytics firebase;
@Before
public void setUp() {
initMocks(this);
/* mock context */
when(analytics.getApplication()).thenReturn(application);
PowerMockito.mockStatic(FirebaseAnalytics.class);
when(FirebaseAnalytics.getInstance(application)).thenReturn(firebase); /* firebase remains null */
PowerMockito.mockStatic(FirebaseAnalytics.class);
}
FirebaseAnalytics is a final class, and getInstance() is a static final method. I'm not sure whether these may make mocking difficult. Any ideas are appreciated!